Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TypeScript · Const

infer

infer captures a type component while a conditional type checks a pattern. The captured variable is usable in the true branch, enabling reusable type-level extraction patterns.

Since
2.8

How it works

Within the true branch of a conditional type, infer introduces a type variable by matching part of the checked type. The inferred variable can then be used in that branch, allowing reusable type-level patterns such as extracting array elements, function return types, tuple members, or pieces of template literal types.

Syntax

T extends U ? X : Y

Key points

  • Use infer in a conditional type to capture a component of a matched type for use in the true branch.
  • The introduced name, such as Item, represents the part extracted by the matching pattern.
  • An inferred variable is available to the successful branch, where it can construct the resulting type.
  • A pattern may infer the same variable in more than one position when the type relationship requires it.

Common mistakes

  • For overloaded function types, inference uses the last signature rather than selecting an overload from argument types.
  • infer belongs in a conditional type's extends pattern, not in an ordinary type-parameter constraint.

Example: Extract an array element type

type ElementType<T> = T extends Array<infer Item> ? Item : T;

type A = ElementType<string[]>; // string
type B = ElementType<number>;   // number

const values: A[] = ["a", "b"];
console.log(values);

The conditional type extracts an array element type and otherwise preserves the input type.

When to use it

Array element extraction

Derive an array's item type or preserve a scalar input.

Function return extraction

Capture a function's result type for a utility alias.

Structural decomposition

Split tuple or template-literal structures into named type components.

Interview questions

QWhat does infer do in a conditional type?

infer declares a type variable inside the extends pattern of a conditional type. If the checked type matches that pattern, the true branch can use the inferred variable; otherwise the false branch is selected.

Follow-up: How would you extract a function's return type? Define T extends (...args: any[]) => infer R ? R : never, then use R in the true branch.

QHow can infer extract an array element type?

type Flatten<T> = T extends Array<infer Item> ? Item : T matches arrays and binds their element type to Item. For a non-array input, the conditional returns the original type.

Follow-up: How can this be generalized to tuples? Match with a pattern such as T extends [infer Head, ...infer Tail] and use the captured types in the true branch.

QWhat limitation applies when inferring from overloaded functions?

For an overloaded function, inference examines the last call signature. The result usually comes from the permissive catch-all signature, and conditional types cannot perform overload resolution from a list of argument types.

Follow-up: What happens when multiple infer locations bind the same variable? TypeScript combines the candidates according to their variance positions, so the resulting type may be a union or intersection.

  • Conditional types
  • Type inference
  • Utility types
  • Template literal types

Source: Conditional Types. Checked 2026-10-15.