0Pricing
TypeScript Academy · Lesson

TupleToUnion and UnionToIntersection

Convert between tuples and unions programmatically.

TupleToUnion and UnionToIntersection is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

TupleToUnion with as const

Combine with as const arrays to derive unions from value arrays.

const COLORS = ["red", "green", "blue"] as const;
type Color = typeof COLORS[number]; // "red" | "green" | "blue"

UnionToIntersection: The Concept

Converting a union to an intersection is harder and requires distributive conditional types with function types to collect all members.

type UnionToIntersection<U> =
  (U extends any ? (x: U) => void : never) extends (x: infer I) => void
    ? I
    : never;

Why the Function Trick Works

Distributing over union members wraps each in a function type. TypeScript then infers the intersection of all parameter types to satisfy the structural constraint.

type A = UnionToIntersection<{ a: 1 } | { b: 2 }>;
// { a: 1 } & { b: 2 }

UnionToTuple: A Hard Problem

Converting a union to a tuple is theoretically possible but relies on evaluation order (which TypeScript does not guarantee) — avoid in production.

// Not reliable — union member order is unspecified
// Use TupleToUnion (safe) not UnionToTuple (fragile)

Practical Use of TupleToUnion

Extract all valid keys from a tuple of string literals to build a union type for validation.

const ALLOWED_METHODS = ["GET", "POST", "PUT", "DELETE"] as const;
type HttpMethod = typeof ALLOWED_METHODS[number];
function request(method: HttpMethod) { /* ... */ }

Practical Use of UnionToIntersection

Merge multiple mixin interfaces from a union into a single combined type.

type Mixin1 = { log(): void };
type Mixin2 = { save(): Promise<void> };
type Combined = UnionToIntersection<Mixin1 | Mixin2>;
// { log(): void } & { save(): Promise<void> }

Intersection of Primitives

Intersecting unrelated primitive types yields never since no value can be both string and number.

type X = UnionToIntersection<string | number>; // never

Using These in Generic Constraints

Combine TupleToUnion and UnionToIntersection inside generic constraints to build flexible, type-safe APIs.

function merge<T extends object[]>(...args: T): UnionToIntersection<T[number]> {
  return Object.assign({}, ...args) as any;
}

Recap: Tuple ↔ Union ↔ Intersection

TupleToUnion uses T[number] indexing. UnionToIntersection uses the distributing-function trick with infer. Both are essential tools for advanced type manipulation.

Quick Check

What is TupleToUnion<[string, number]>?

What You Learned

TupleToUnion uses numeric indexing to extract element unions from tuples. UnionToIntersection uses the distributing-function trick. These are foundational patterns for advanced TypeScript type manipulation.

Frequently asked questions

Is the “TupleToUnion and UnionToIntersection” lesson free?

Yes — the full text of “TupleToUnion and UnionToIntersection” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “TupleToUnion and UnionToIntersection”?

Convert between tuples and unions programmatically. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “TupleToUnion and UnionToIntersection” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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