-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Union type to "unordered" array #57321
Comments
I can't think of something you could do with this that you can't already do today. "Let me do the thing I'm asking for" is not a motivating example; it's begging the question. What are you trying to do? |
Thanks Ryan for the feedback. My apologies if the intent was not clear enough. When your backend exposes a GraphQL schema, then you can generate all the types exposed by its schema. In our case we use the Relay compiler for that purpose and it generates the TypeScript types. This gives us strong guarantees. type Language = "English" | "French" | "Italian" | "German" We then offer all these languages into a dropdown list and the user of our application can choose one language from it. It could be due to our lack of knowledge, I cannot be 100% sure, but right now we would create an array containing all possible languages, and we would create the dropdown entries from it. As such our code looks like: const languages: Language[] = ["English", "French", "Italian", "German"]
// Then use languages to generate dropdown entries If we could generate such arrays from the union type members, then this step would be eliminated. |
So you're trying to generate a runtime-accessible array from a type definition? Yeah, that's not going to happen; it violates
You always have to do the reverse: write the array, then infer a type from it. const languages = ["English", "French", "Italian", "German"] as const;
type Language = typeof languages[number];
// ^? |
We don't generate runtime code from type information, ever - that's a key design constraint. I think you'd be interested to read #53171 - this is the use case as far as I understand |
You should instead modify the generator to generate the runtime array, that's what I do when generating typescript code from an OpenAPI schema. |
Thanks @RyanCavanaugh and @fatcerberus you nailed it, it would be against TS goals. |
π Search Terms
union type to array
β Viability Checklist
β Suggestion
<option>
of your<select>
for all union type members, then you would most likely sort ascending anywayHaving 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 text was updated successfully, but these errors were encountered: