The infer Keyword

infer introduces a type variable inside a conditional type pattern. It lets you extract parts of a type, such as a function's return type, an array element type, or a promise's resolved type. Built-in utility types such as ReturnType and Awaited use related patterns.

Keep the pattern aligned with the shape you want to inspect. For overloaded functions, ReturnType generally uses the last signature; do not assume it represents every overload separately. Type-level inference is compile-time computation and does not call the function or inspect a runtime value.

Extract a promise's resolved value
type Resolved<T> = T extends Promise<infer Value> ? Value : T;

type A = Resolved<Promise<string>>; // string
type B = Resolved<number>;          // number
💡 If T matches Promise<...>, infer Value captures the inner type. Otherwise the conditional returns T unchanged.
Key Takeaways
  • infer captures a type within a conditional pattern.
  • It operates on types, not runtime values.
  • Built-in utility types use similar extraction patterns.
Knowledge Check

What does `infer Value` do in `T extends Promise<infer Value>`?