What is the infer keyword in TypeScript

· Category: TypeScript

Short answer

infer declares a type variable inside a conditional type that captures a type from a matched structure, enabling powerful type extraction utilities.

How it works

Inside a extends clause, infer X tells TypeScript to infer the type X from the type being tested. It is only valid in the extends position of a conditional type. You can infer multiple types and use them in the true branch.

Example

type ReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
type PromiseValue<T> = T extends Promise<infer V> ? V : never;

Why it matters

infer is the foundation of advanced utility types. It allows you to reverse-engineer function signatures, unwrap promises, and extract element types from arrays without explicit type parameters.