Course contentsShow
← TypeScript
Lesson 605 of 1,79614. Generics in PracticeFree lesson

Inference with Multiple Type Parameters

How TypeScript infers multiple generic type parameters and the order of inference resolution.

Inference with Multiple Type Parameters

What you'll learn: How TypeScript infers multiple generic type parameters and the order in which it resolves them.

The Core Idea

When a function has multiple type parameters, TypeScript infers them one at a time, left to right, using information from the arguments you pass. Each type parameter's inference can depend on the ones before it (if they're constrained), and TypeScript tries to solve them all simultaneously based on the argument types.

Think of it like filling in blanks in a sentence: if you say "I put the ___ in the ___," TypeScript looks at the actual objects you're holding to figure out what goes in each blank. With multiple type parameters, it's solving a small puzzle where each piece of information helps narrow down the unknowns.

How Inference Resolution Works

TypeScript examines each argument position and attempts to match it against the corresponding parameter type. When you have something like <T, U>, TypeScript will:

  1. Look at the first argument to infer T
  2. Look at the second argument to infer U
  3. Cross-reference if parameters use each other (like T appearing in U's constraint)

The inference happens all at once during type checking, but logically flows through the parameter list. If an argument provides no useful information for a type parameter, that parameter might remain unresolved (and you'll get an error or need to specify it explicitly).

Key Takeaway: TypeScript infers multiple type parameters by analyzing all arguments together, matching each parameter's type expectations left-to-right, and can handle dependencies between type parameters during the resolution process.