Conditional type

Selects one of two types based on a type relationship test.

Since TS 2.8 Spec ↗

Syntax

T extends U ? X : Y

Examples

type IsArray<T> = T extends any[] ? true : false;
type A = IsArray<number[]>; // true
type B = IsArray<number>; // false
type Flatten<T> = T extends (infer U)[] ? U : T;
type F = Flatten<string[]>; // string

Notes

When the checked type is a naked type parameter, conditional types distribute over union members. Wrap both sides in tuples (`[T] extends [U]`) to opt out of distribution. Combine with `infer` to extract types from patterns.

See also