Conditional Types in TypeScript
Branch types at compile time with T extends U ? X : Y and the infer keyword.
What you'll learn
- How T extends U ? X : Y selects types based on a condition
- Using the infer keyword to extract types from generic parameters
- Distributive conditional types over unions
- Building reusable type-level utilities with conditionals
Concept
The Conditional Syntax
A conditional type selects between two types based on whether one extends another: T extends U ? X : Y. If T is assignable to U, the result is X; otherwise, it is Y. This is like a ternary expression but at the type level. For example, type IsString<T> = T extends string ? true : false produces true when T is a string and false otherwise. Conditional types let you write logic that the compiler evaluates during type checking.
The infer Keyword
The infer keyword extracts a type from within another type, but only inside the extends clause of a conditional. For example, type Unbox<T> = T extends Array<infer U> ? U : T extracts the element type if T is an array, otherwise returns T. This is how ReturnType<T> is implemented: T extends (...args: any) => infer R ? R : never extracts the return type R from a function type. The infer keyword is essential for unwrapping and reverse-engineering types.
Distributive Conditionals
When a conditional type's checked type is a naked type parameter (not wrapped in another type), it distributes over unions. T extends U ? X : Y with T = A | B evaluates as (A extends U ? X : Y) | (B extends U ? X : Y). This distribution is usually what you want, but it can be surprising. To prevent distribution, wrap the type parameter: [T] extends [U] ? X : Y.
Example
// Basic conditional — check if T is a string.
type IsString<T> = T extends string ? true : false;
type A = IsString<"hello">; // true
type B = IsString<42>; // false
// infer — extract the element type of an array.
type ElementOf<T> = T extends (infer U)[] ? U : never;
type Num = ElementOf<number[]>; // number
type Str = ElementOf<string[]>; // string
// infer — extract return type of a function.
type MyReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
type R = MyReturnType<() => string>; // string
// Distributive conditional — applies to each union member.
type ToArray<T> = T extends any ? T[] : never;
type Result = ToArray<string | number>; // string[] | number[]
// Non-distributive — wrap in a tuple to prevent distribution.
type NonDist<T> = [T] extends [any] ? T[] : never;
const check: A = true;
const elem: Num = 42;
console.log(check, elem);
Conditional types (T extends U ? X : Y) branch at the type level. The infer keyword extracts types from generic parameters. Naked type parameters distribute over unions; wrapping in tuples prevents distribution.
Try it
- JSON to TypeScript
Conditional types refine generated types based on field shapes.
- Code Diff
Compare type-level logic with its runtime equivalent.
Common mistakes
The mistake
Expecting conditional types to evaluate at runtime
The fix
Conditional types are compile-time only. They produce a type, not a value. For runtime branching, use regular if/else or switch statements with type guards.
The mistake
Forgetting that naked type parameters distribute over unions
The fix
T extends U ? X : Y distributes when T is a naked parameter over a union. Wrap in [T] extends [U] to prevent distribution if you want to treat the union as a whole.
Practice Conditional Types in TypeScript
6 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.