Rest Elements with infer
What you'll learn: How to capture the "tail" of a tuple or function parameters using [...infer R] syntax within conditional types.
The Core Idea
You've seen how infer can extract single elements from tuples—like the first or last item. But what if you want to grab everything else? That's where rest elements with infer come in.
By combining infer with the rest syntax [...infer R], you can capture multiple remaining elements as a new tuple type. This is incredibly powerful for:
- Extracting the tail of a tuple (all elements except the first)
- Extracting the head of a tuple (all elements except the last)
- Capturing variadic function parameters after skipping some
How It Works
The syntax looks like this:
type Tail<T> = T extends [infer First, ...infer Rest]
? Rest
: never;
Here's what happens:
infer Firstcaptures the first element...infer Restcaptures all remaining elements as a tuple- We return
Rest, which is the tail
Real example:
type Example = Tail<[1, 2, 3, 4]>;
// Result: [2, 3, 4]
type SingleElement = Tail<[string]>;
// Result: []
type Empty = Tail<[]>;
// Result: never (doesn't match the pattern)
Practical Pattern: Extracting Parameter Tails
You can use this with function parameter types:
type SkipFirstParam<T> = T extends (first: any, ...rest: infer R) => any
? R
: never;
type Fn = (name: string, age: number, active: boolean) => void;
type Remaining = SkipFirstParam<Fn>;
// Result: [age: number, active: boolean]
This captures everything after the first parameter, preserving labels and types.
Combining Head and Tail Extraction
// Get everything except the last element
type Init<T extends readonly any[]> =
T extends [...infer Head, infer Last]
? Head
: never;
type Example = Init<[1, 2, 3, 4]>;
// Result: [1, 2, 3]
Notice how ...infer Head comes first—TypeScript's type system figures out where to split based on position.
Key Takeaway: Rest elements with infer ([...infer R]) let you capture the remaining parts of tuples or function parameters, enabling powerful tuple manipulation utilities and type-safe variadic patterns.