Created
July 20, 2022 12:43
-
-
Save andrekovac/c1062182381554cecd80fb9602d40647 to your computer and use it in GitHub Desktop.
Two ways to create a a TupleToUnion type in TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Two ways to create a a TupleToUnion type in TypeScript | |
*/ | |
export type TupleToUnion<T extends unknown[]> = T[number]; | |
export type TupleToUnion2<T extends unknown[]> = T extends [ | |
infer U, | |
...infer Rest | |
] | |
? TupleToUnion2<Rest> | U | |
: never; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also the corresponding challenge in the type-challenges repo: