inference

infer R

A keyword usable only inside the extends clause of a conditional type that introduces a fresh type variable inferred from the matched shape. It is the primitive that powers ReturnType, Parameters, InstanceType, Awaited, and countless custom utilities.

T extends SomeShape<infer R> ? R : Fallback

Usage patterns

Pattern Purpose
infer R Introduce a fresh inferred type variable
extends (...a: any) => infer R Infer a function's return type
extends Array<infer E> Infer an element type from an array
extends `${infer H}-${infer T}` Infer parts from a template literal type

Examples

type ElementOf<T> = T extends (infer E)[] ? E : never;
type E = ElementOf<number[]>;

Result: number — infer used on tuple/array shapes

type First<T extends any[]> = T extends [infer F, ...any[]] ? F : never;

Grabs the head of a tuple with rest inference

type Split<S> = S extends `${infer H}.${infer T}` ? [H, ...Split<T>] : [S];

Recursive string-splitting via template literal inference

type R = (() => number) extends () => infer X ? X : never;

R is `number` — the classic ReturnType pattern. Without the parens the arrow parses as a function type whose body is the conditional, giving `() => never` instead.

Gotcha

Only valid inside the `extends` clause of a conditional type — using `infer` elsewhere is a syntax error. Multiple `infer` sites for the same name in co-variant positions yield a union; in contra-variant positions they yield an intersection.

Related utility types

← All TS utility types · TypeScript vs JavaScript · any vs unknown vs never