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

Partial Inference and Mixed Specification

Understanding that you must specify all or none of the type arguments when some cannot be inferred.

Partial Inference and Mixed Specification

What you'll learn: Why TypeScript forces you to specify either all or none of the type arguments when some cannot be inferred automatically.

The All-or-Nothing Rule

Imagine you're ordering a custom sandwich, but the shop has a strict policy: either you specify every single ingredient, or you let the chef choose everything based on what you've ordered before. You can't say "I want turkey, but you pick the cheese and bread."

TypeScript has a similar rule with generics: you cannot specify some type arguments explicitly while letting TypeScript infer others. It's all manual or all automatic.

Why This Limitation Exists

When TypeScript sees a function call with generic type parameters, it makes a choice:

  • If you provide angle brackets with type arguments, TypeScript expects you to specify all of them in order
  • If you omit angle brackets, TypeScript tries to infer all type parameters from your arguments

There's no middle ground where you say "infer the first one, but I'll tell you the second one."

function pair<T, U>(first: T, second: U) {
  return [first, second];
}

// ✅ Full inference
const a = pair(10, "hello");

// ✅ Full specification
const b = pair<number, string>(10, "hello");

// ❌ Cannot do this - partial specification not allowed
// const c = pair<number>(10, "hello");

Even though TypeScript could theoretically infer U from "hello", the syntax doesn't allow mixing the two approaches.

Practical Implications

When inference fails for one parameter but works for another, you're stuck: you must specify everything explicitly, even the types TypeScript could have figured out on its own.

Key Takeaway: TypeScript requires you to specify all type arguments or none — partial inference with mixed explicit/implicit specification is not supported. Either use angle brackets for everything or let inference handle it all.