-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
DeclinedThe issue was declined as something which matches the TypeScript visionThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
π Search Terms
union type to array
β Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β Suggestion
- This feature has been rejected several times (see: Type manipulations: union to tupleΒ #13298) due to the premise that a union type is unordered and cannot be converted into a Tuple or an Array as these have a given order.
- While this is true the reality is that we often need to create arrays that contain all possible union type members, without caring about their positions within such arrays. Examples:
- you want to generate all possible
<option>
of your<select>
for all union type members, then you would most likely sort ascending anyway - for validation purpose with libs like zod
- you want to generate all possible
- Different folks also suggested that instead of trying to get an array out of a union type, you could do the opposite: create the array of possibilities, and generate the union type out of it.
- Unfortunately when working with schemas delivered by a backend and using tools to generate types then we gain strong guarantees but loose the ability to customize the type definition. Such type generation tools would often create union types.
Having a way to transform a union type into an "unordered" array would remove a ton of boilerplate we need to write now.
π Motivating Example
Allow iterating on union types, by transforming the union type into an array.
π» Use Cases
- The main use case is when you are not in control of type generation, e.g. a schema delivered from a backend (OpenAPI, GraphQL, etc)
MartinJohns and RyanCavanaugh
Metadata
Metadata
Assignees
Labels
DeclinedThe issue was declined as something which matches the TypeScript visionThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScriptAn idea for TypeScript