Skip to content

Tuple Rest

In one line

Tuple rest (...T[]) enables tuples with fixed leading elements and a variable number of trailing elements.

In simple words

Tuple rest elements let you define tuples with a fixed prefix and a variable-length suffix. The syntax is [A, B, ...T[]] where A and B are fixed and T[] is the rest.

This enables type-safe variadic functions — functions that accept a fixed set of initial arguments followed by any number of additional arguments.

TypeScript 4.0+ supports labeled tuple elements and spread in tuples, making them powerful for function parameter types. Combined with infer, you can extract the rest portion of a tuple.

Example

typescript
// Variadic tuple — first is string, rest are numbers
type Scores = [string, ...number[]];

const valid: Scores = ["Alice", 90, 85, 92];
// const invalid: Scores = ["Bob", 90, "oops"]; // Error!

// Function with typed rest parameters
function concat<T extends readonly unknown[]>(...args: [...T]): T {
  return args;
}
const result = concat(1, "hello", true); // type: [number, string, boolean]

// Labeled tuple elements
type Pair = [first: number, second: string];

[string, ...number[]] is a tuple with a string first element and any number of numbers after. Labels make tuple positions self-documenting.

Common confusions

  • Confused with: array

    The difference: An array type T[] has any number of elements all of type T. A tuple [A, B, ...T[]] has fixed types at specific positions plus a variable tail. Tuples are fixed-length (with rest for variadic); arrays are homogeneous and any length.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary