TupleToUnion and UnionToIntersection
Convert between tuples and unions programmatically.
TupleToUnion: The Concept
TupleToUnion converts a tuple type into a union of its element types — the reverse of building a tuple from individual types.
type TupleToUnion<T extends readonly any[]> = T[number];
type A = TupleToUnion<[string, number, boolean]>; // string | number | booleanHow T[number] Works
Indexing a tuple with number accesses all numeric indices at once, which TypeScript expresses as a union of element types.
type Tuple = [string, number, boolean];
type Elements = Tuple[number]; // string | number | booleanAll lessons in this course
- DeepPartial and DeepReadonly
- Flatten and UnwrapPromise Utilities
- TupleToUnion and UnionToIntersection
- Publishing a Type Utility Library