Course contentsShow
← TypeScript
Lesson 1394 of 1,79632. Advanced PatternsFree lesson

Variadic Tuples vs Rest Parameters

Understanding when variadic tuple types provide stricter typing than traditional rest parameters.

Variadic Tuples vs Rest Parameters

What you'll learn: How variadic tuple types provide stricter type guarantees than traditional rest parameters, and when to choose each approach.


The Core Difference

Both variadic tuples and rest parameters let you work with variable numbers of arguments, but they differ fundamentally in type precision:

  • Rest parameters (...args: T[]) treat all arguments as an array of the same type
  • Variadic tuples (...args: [...T]) preserve the exact position, type, and length of each argument

Think of rest parameters as saying "any number of items of this type," while variadic tuples say "exactly these items in this order."

Rest Parameters: Loose but Simple

function logAll(...messages: string[]) {
  messages.forEach(m => console.log(m));
}

logAll("hello", "world");  // Fine
logAll();                   // Also fine - empty array

Here, TypeScript knows messages is a string array, but it doesn't track how many strings or their individual positions. This is flexible but loses information.

Variadic Tuples: Strict and Precise

function process<T extends unknown[]>(...args: [...T]): [...T] {
  return args;
}

const result = process("hello", 42, true);
// result is typed as [string, number, boolean] - exact types preserved!

With variadic tuples, TypeScript remembers the exact type of each position. The return type mirrors the input structure perfectly.

When Variadic Tuples Win

Variadic tuples shine when you need to:

  • Preserve exact argument order and types (like in compose or curry functions)
  • Transform tuple structures while maintaining type relationships
  • Enforce specific arity constraints at the type level
  • Pass through argument lists with full type fidelity

When rest parameters would give you string[], variadic tuples give you [string, string, string] — a major difference for type safety.

Key Takeaway: Use rest parameters when you truly need homogeneous, length-agnostic collections; use variadic tuples when argument position and type matter, and you want TypeScript to track every detail of your function's signature structure.