0Pricing
TypeScript Academy · Lesson

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 | boolean

How 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 | boolean

All lessons in this course

  1. DeepPartial and DeepReadonly
  2. Flatten and UnwrapPromise Utilities
  3. TupleToUnion and UnionToIntersection
  4. Publishing a Type Utility Library
← Back to TypeScript Academy