Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Generics

Type argument inference in TypeScript

How TypeScript infers generic type arguments: candidates from arguments and return types, widening, contextual typing, inference priority and when to annotate.

When you call a generic function without writing type arguments, the compiler infers them. It looks at the arguments you passed, collects candidate types for each type parameter, and picks the best one. If it finds no candidates it uses the parameter's default or constraint, and finally unknown.

Understanding the rules explains most surprises: why T becomes string instead of "a", why two arguments can conflict, and why an explicit type argument sometimes appears necessary.

TypeScript
function pick<T>(a: T, b: T): T { return a; }

pick(1, 2);        // T = number
pick("a", "b");    // T = string

Try it

TypeScript Candidates, widening and conflicts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Contextual typing and return-type inference

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

Inference has no syntax of its own; it is what happens when type arguments are omitted. You override it by writing arguments:

TypeScript
fn(arg);                    // inferred
fn<Explicit>(arg);          // explicit, all-or-nothing
const result: Wanted = fn(arg);  // return-type position can contribute candidates
Source of candidates Description
Argument types The main source. Each argument whose parameter mentions T adds a candidate.
Contextual return type An annotation or expected type at the call site can add a lower-priority candidate.
Callback return values Return types of function arguments can infer the type parameters used in their return position.
Constraint and default Used when no candidates exist.

Rules: with several candidates for a covariant position, the compiler picks the best common supertype, and if none of the candidates is a supertype of all, reports an error. Candidates from contravariant positions (function parameters) are intersected instead.

Usage notes

  • Widening. A candidate from a literal such as "a" is widened to string unless the type parameter has a constraint that includes primitives (T extends string) or you use a const type parameter (see const type parameters).
  • Order matters for callbacks. The compiler infers from non-callback arguments first, so mapAll(items, (s) => ...) knows s before it checks the lambda. Put the data before the callback.
  • No partial inference. Writing fn<A>(...) for a function with <A, B> does not infer B. Either give both or restructure with currying (see Higher-order generic functions).
  • Block unwanted candidates with NoInfer<T> when one argument should check against T but not contribute to it.
  • unknown is the fallback, not any. A result typed unknown is the signal that inference found nothing.
  • Hover to debug. The editor shows the inferred signature of each call; mismatches there usually point to the conflicting argument.
  • Generic classes infer from constructor arguments, but a bare new Stack() with no arguments gives Stack<unknown>.