Conditional Types (intro) & distribution over unions
Learn conditional types (T extends U ? X : Y) and how they distribute over unions for powerful type-level logic.
Conditional Types (intro) & distribution over unions is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Goal: Use conditional types (T extends U ? X : Y) to branch at the type level, and understand distribution over unions for precise results.
Basic conditional
A conditional type returns one of two types based on whether T extends U holds.
type IsString<T> = T extends string ? true : false;
let a: IsString<string>; // true
let b: IsString<number>; // false
Distribution
With a naked type parameter T, the conditional runs on each union member, then unions the results.
type ToArray<T> = T extends any ? T[] : never;
type R1 = ToArray<string | number>; // string[] | number[]Stop distribution
Wrap T in a tuple (e.g., [T]) to stop distribution and treat the union as a whole.
type ToArrayNoDistrib<T> = [T] extends [any] ? T[] : never;
type R2 = ToArrayNoDistrib<string | number>; // (string | number)[]Practical Exclude
Many utility types rely on distribution (e.g., Exclude, Extract). You can build your own variants easily.
type MyExclude<T, U> = T extends U ? never : T;
type R3 = MyExclude<"a" | "b" | "c", "b">; // "a" | "c"Tips
Tips:
- Distribution happens only with a naked type parameter on the left of
extends. - Wrap in a tuple to opt out.
- Test with small examples to confirm intuition.
Distribution check
Quick check: Why do conditional types distribute over unions?
Recap
Recap: Use conditional types for branching at the type level. Understand when results distribute over unions and how to disable it with tuple wrapping.
Frequently asked questions
Is the “Conditional Types (intro) & distribution over unions” lesson free?
Yes — the full text of “Conditional Types (intro) & distribution over unions” is free to read here on the web, and the TypeScript Academy course includes 3 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 “Conditional Types (intro) & distribution over unions”?
Learn conditional types (T extends U ? X : Y) and how they distribute over unions for powerful type-level logic. 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 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Conditional Types (intro) & distribution over unions” 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
- Generic interfaces & type aliases
- Conditional Types (intro) & distribution over unions
- Reusable patterns for data models