Conditional Types
A conditional type picks one of two types by testing assignability, written T extends U ? A : B. It is a type-level if.
Detailed Explanation
Value-level code branches on values. Type-level code branches on types. A conditional type asks: is T assignable to U? If yes, the result is A. If no, the result is B.
type IsString<T> = T extends string ? true : false;
IsString<"ok"> is true. IsString<number> is false. The test is the same extends used in generic constraints: assignability, not class inheritance.
The feature that makes conditionals powerful is distributivity. When the checked type is a naked type parameter, the conditional runs once per member of a union and unions the results. IsString<string | number> is true | false, not false. That is how Exclude, Extract, and NonNullable are written: they filter union members by keeping or dropping each one.
A "naked" type parameter means T, not T[], not Promise<T>, not [T]. Wrapping T turns distribution off. T extends any ? X : never distributes; [T] extends [any] ? X : never does not. You choose the wrapping depending on whether you want to filter a union or treat it as one unit.
The true and false branches can be conditional themselves, which is how you write type-level pattern matching. Combined with infer, a conditional can pull a piece out of a type: the element of an array, the resolved value of a promise, the return type of a function. Those extractions are the built-in ReturnType, Awaited, and Parameters utilities.
Distributive conditionals plus never are the filter idiom. T extends string ? T : never keeps string members and drops the rest, because never disappears from unions. That idiom shows up in almost every non-trivial utility type.
Costs are real. A chain of conditionals on a large union is work the checker does at every use. Error messages print the whole chain. Recursion through conditionals has a depth limit, and a slip can hit "type instantiation is excessively deep." Prefer a named intermediate type over a one-line nest of four extends.
Conditional types are erased. They do not run at runtime and they do not replace a type guard. Use them to describe a result you have already computed in JavaScript, not to pretend the compiler will pick a branch when the program runs.
Code Examples
1. A simple test, and distribution over a union.
// 1. A simple test, and distribution over a union.
type IsString<T> = T extends string ? true : false;
type A = IsString<"ok">; // true
type B = IsString<number>; // false
type C = IsString<string | number>; // true | false2. Filtering a union — this is Exclude.
// 2. Filtering a union — this is Exclude.
type DropNumbers<T> = T extends number ? never : T;
type Ids = DropNumbers<string | number | boolean>; // string | boolean
type OnlyNumbers<T> = T extends number ? T : never;
type Counts = OnlyNumbers<string | 1 | 2>; // 1 | 23. Turning distribution off by wrapping, and a nested match.
// 3. Turning distribution off by wrapping, and a nested match.
type Whole<T> = [T] extends [string | number] ? "yes" : "no";
type D = Whole<string | boolean>; // "no" — the union as a unit is not assignable
type Unwrap<T> =
T extends Promise<infer U> ? Unwrap<U> :
T extends (infer U)[] ? U :
T;
type E = Unwrap<Promise<Promise<string[]>>>; // stringCommon Gotchas
Naked `T extends` distributes; wrapped `[T] extends` does not
if a utility suddenly returns a union of results you did not expect, you are distributing. Wrap the parameter to treat the union as one type.
`never` as the input distributes to nothing
`T extends X ? A : B` with `T = never` becomes `never`, not `B`. Special-case `never` if that matters.
The test is assignability
`string extends "ok" ?` is false, `"ok" extends string ?` is true. Direction matches `extends` in constraints.
Deep recursive conditionals hit a compiler limit
add a depth counter or flatten the structure if you see "excessively deep" errors.