infer

Declares a type variable to be inferred within a conditional type.

Since TS 2.8 Spec ↗

Syntax

T extends SomePattern<infer U> ? U : never

Examples

type ElementOf<T> = T extends (infer U)[] ? U : never;
type N = ElementOf<number[]>; // number
type Return<T> =
  T extends (...args: any[]) => infer R ? R : never;
type R = Return<() => string>; // string

Notes

`infer` can only appear in the `extends` clause of a conditional type. It introduces a fresh type variable captured by pattern matching. Multiple `infer` positions for the same variable in a covariant position produce a union; in a contravariant position they produce an intersection.

See also