A conditional type T extends U ? X : Y chooses a type based on assignability, like a ternary at the type level. Inside the extends clause, infer R declares a type variable that TypeScript fills in by pattern-matching, so you can extract parts of a type: a function's return type, a promise's resolved value, an array's element type, or a segment of a string literal. When T is a naked type parameter, conditional types distribute over unions. Many built-in utilities (ReturnType, Parameters, Awaited, Exclude, NonNullable) are conditional types.
type IsString<T> = T extends string ? true : false;
type A = IsString<'hi'>; // true
type B = IsString<42>; // false
type MyReturnType<F> = F extends (...args: any[]) => infer R ? R : never;
type ElementType<T> = T extends readonly (infer E)[] ? E : T;
type PromiseValue<T> = T extends Promise<infer V> ? V : T;
type R1 = MyReturnType<() => Date>; // Date
type E1 = ElementType<string[]>; // string
type P1 = PromiseValue<Promise<number>>; // number
infer with a constraint (TS 4.7+) narrows what's captured:
type FirstString<T> = T extends [infer S extends string, ...unknown[]] ? S : never;
type F = FirstString<['a', 1]>; // 'a'
Template literal pattern matching:
type RouteParams<P extends string> =
P extends `${string}:${infer Param}/${infer Rest}`
? Param | RouteParams<`/${Rest}`>
: P extends `${string}:${infer Param}`
? Param
: never;
type Params = RouteParams<'/users/:userId/posts/:postId'>; // 'userId' | 'postId'
type ToArray<T> = T extends unknown ? T[] : never;
type D = ToArray<string | number>; // string[] | number[] (distributed)
type ToArrayND<T> = [T] extends [unknown] ? T[] : never;
type ND = ToArrayND<string | number>; // (string | number)[] (wrapping in a tuple prevents distribution)
Distribution is why Exclude<'a' | 'b' | 'c', 'a'> gives 'b' | 'c'. Note that never distributes to never: IsString<never> is never, not false. Wrap it in a tuple if you need to detect never.
type Props = Parameters<typeof createUser>[0].Awaited<ReturnType<typeof api.getUser>>.DeepPartial, Awaited's recursive unwrapping of nested thenables).expectTypeOf, // @ts-expect-error checks).T is an unresolved generic, the conditional stays unevaluated inside a function body, so return statements often need casts. Overloads can be cleaner.infer candidates: inferring from overloaded functions uses the last overload signature.T extends U ? X : Y as type-level branching, and infer as pattern matching.[T] trick to disable it.ReturnType, Awaited and Exclude are implemented.Spotted a mistake or a rights concern? Report an issue