Course contentsShow
← TypeScript
Lesson 794 of 1,79618. Conditional TypesFree lesson

Multiple infer in One Conditional

Using multiple infer keywords in the same extends clause to capture different parts of a complex type.

Multiple infer in One Conditional

You'll learn: How to use multiple infer keywords in the same conditional type to capture several pieces of a complex type at once.

The Core Idea

Just like extracting one piece of a type with infer, you can use multiple infer declarations in a single extends clause to capture different parts of the same type simultaneously.

Think of it like pattern matching with multiple variables: instead of matching just one unknown, you're matching several unknowns in one expression and naming each one.

Why You Need Multiple infers

When a type has multiple components—like a function with parameters and a return type, or a tuple with multiple elements—you often want to extract more than one piece at the same time.

Basic Syntax Pattern

type MyConditional<T> = T extends SomePattern<infer A, infer B>
  ? [A, B]  // Use both A and B in the true branch
  : never;

Notice: two separate infer keywords, each creating a distinct type variable you can reference in the true branch.

Example: Function Parameters and Return Type

type FunctionParts<T> = T extends (a: infer A, b: infer B) => infer R
  ? { param1: A; param2: B; returnType: R }
  : never;

type Example = FunctionParts<(x: string, y: number) => boolean>;
// Result: { param1: string; param2: number; returnType: boolean }

Here we extract three parts: the two parameter types and the return type, all in one conditional.

Example: Tuple Destructuring

type FirstTwo<T> = T extends [infer First, infer Second, ...any[]]
  ? [First, Second]
  : never;

type Result = FirstTwo<[string, number, boolean, symbol]>;
// Result: [string, number]

We capture the first two elements individually while ignoring the rest.

Same Type Variable Can Appear Multiple Times

If you use the same infer name twice, TypeScript will infer a union or intersection depending on variance:

type Duplicate<T> = T extends (a: infer X, b: infer X) => void
  ? X
  : never;

type Test = Duplicate<(a: string, b: number) => void>;
// X becomes: string | number (union of both positions)

Key Takeaway: Multiple infer keywords let you extract several type pieces from one pattern simultaneously, enabling powerful type transformations like deconstructing function signatures or tuples in a single conditional expression.