0Pricing
TypeScript Academy · Lesson

Tuple Rest Elements

Use rest elements inside tuple types.

Variable-Length Tuples

Tuples normally have a fixed length, but a rest element lets one position absorb any number of items. This models data that is partly fixed and partly variable.

A Trailing Rest Element

Write [string, ...number[]] to mean: one string, then zero or more numbers. The first slot is fixed; the rest grows freely.

type Row = [string, ...number[]]
const r: Row = ["scores", 9, 8, 7]
console.log(r[0], r.length) // scores 4

All lessons in this course

  1. Rest Parameters in Functions
  2. Spread in Arrays and Objects
  3. Tuple Rest Elements
  4. Typing Variadic Functions
← Back to TypeScript Academy