Skip to content

Union type to "unordered" arrayΒ #57321

@molinch

Description

@molinch

πŸ” Search Terms

union type to array

βœ… Viability Checklist

⭐ 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
  • 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions