Expert15 minType Systems
UpdatedAug 1, 2026
Edit

The infer Keyword

Question Variations

  • "What does the `infer` keyword do in TypeScript conditional types?"
  • "Can you write a utility type that extracts the return type of a function using `infer`?"
  • "Where is the `infer` keyword allowed to be used in a TypeScript type definition?"
  • "How does `infer` help in creating flexible, generic library APIs?"

Why This Is Asked

infer is an advanced conditional type feature that separates TypeScript power users from casual users. Interviewers use this to assess whether you can build utility types, understand conditional type mechanics, and read complex generic signatures in library code.

Key Concepts

  • infer declares a type variable within the extends clause of a conditional type, capturing a sub-type
  • The inferred variable is only available in the true branch of the conditional
  • Powers built-in utility types: ReturnType<T>, Parameters<T>, Awaited<T>, InstanceType<T>
  • Can extract types from any position: function return types, promise inner types, array element types, tuple positions
  • Advanced: multiple infer positions, recursive conditional types, template literal type extraction

Question Variations

  • “What does the infer keyword do in TypeScript conditional types?”
  • “Can you write a utility type that extracts the return type of a function using infer?”
  • “Where is the infer keyword allowed to be used in a TypeScript type definition?”
  • “How does infer help in creating flexible, generic library APIs?”

Answers by Technology

+ Add Variant
TypeScriptImprove this answer ✏️

Expected Answer

The infer keyword is used within Conditional Types to declare a type variable that TypeScript will attempt to “calculate” (infer) based on the input type. It allows you to extract sub-types from a larger structure like a function’s return type, a Promise’s resolution, or an array’s element type.

It can only be used in the extends clause of a conditional type and the inferred variable is only available in the “true” branch.

Why It Matters

infer is the engine behind TypeScript’s meta-programming. It is used in nearly all built-in utility types (like ReturnType, Awaited, Parameters). Without infer, we couldn’t write generic decorators, wrappers, or API clients that automatically know the shape of the data they are handling based on a provided function or promise.

Example Code

Extracting a Promise Result

type UnpackPromise<T> = T extends Promise<infer U> ? U : T;

type Result = UnpackPromise<Promise<number>>; // number
type Direct = UnpackPromise<string>;           // string

Extracting Function Return Type

type GetReturnType<T> = T extends (...args: any[]) => infer R ? R : never;

const myFunc = () => ({ id: 1 });
type MyFuncReturn = GetReturnType<typeof myFunc>; // { id: number }

Common Mistakes

  • Using infer outside of conditional types: It is syntactically invalid outside of a T extends ... ? block.
  • Scope issues: Trying to use the inferred type in the “false” branch of the conditional, where it is not defined.
  • Complexity: Over-nesting infer blocks, making types nearly impossible for teammates to read or debug.

Follow-up Questions

  • How do you infer from an array? (Answer: T extends (infer U)[] ? U : never).
  • What happens if multiple positions match the inference? (Answer: If you infer from multiple positions (like multiple arguments of a function), TS will usually create a union or intersection depending on the variance).

References