Tuple Rest Elements
Use rest elements inside tuple types.
Tuple Rest Elements is a free TypeScript Academy lesson on CoddyKit. This is lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The TypeScript Academy course includes 4 lessons in total.
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 4The Fixed Part Is Required
The leading fixed elements must be present. The rest portion may be empty, but the fixed slots cannot be skipped.
type Row = [string, ...number[]]
const min: Row = ["label"] // ok, zero numbers
// const bad: Row = [] // Error: missing string
console.log(min)Leading Rest Element
A rest element can also come first, with fixed elements after it. This models a variable head followed by a fixed tail.
type Trailing = [...number[], boolean]
const t: Trailing = [1, 2, 3, true]
console.log(t[t.length - 1]) // trueMiddle Rest Element
The rest can sit in the middle, between fixed leading and trailing elements. TypeScript figures out which positions are fixed at each end.
type Middle = [string, ...number[], boolean]
const m: Middle = ["start", 1, 2, false]
console.log(m[0], m[m.length - 1]) // start falseOnly One Rest Per Tuple
A tuple may contain at most one rest element. Two rests would make element positions ambiguous, so it is disallowed.
// type Bad = [...string[], ...number[]] // Error: only one rest allowed
type Ok = [string, ...number[]]
const o: Ok = ["x", 1]
console.log(o)Destructuring a Rest Tuple
Destructuring lines up perfectly with tuple rest elements. Fixed bindings take the fixed slots; a rest binding captures the variable part as a typed array.
type Row = [string, ...number[]]
const row: Row = ["scores", 9, 8]
const [label, ...values] = row
console.log(label, values) // scores [9, 8]Labeled Tuple Elements
You can name tuple positions for documentation. Labels appear in tooltips and signatures but do not change the runtime values.
type Range = [start: number, end: number]
const span: Range = [0, 10]
console.log(span[0], span[1]) // 0 10Labels on Rest Elements
Rest elements can be labeled too, which is especially helpful in function parameter tuples for readable hints.
type Args = [command: string, ...flags: string[]]
const a: Args = ["build", "--watch", "--minify"]
console.log(a[0], a.slice(1))Rest Tuples as Parameter Lists
A tuple with a rest element can describe a function's parameter list, enabling precise fixed-then-variadic signatures via spread.
type Params = [name: string, ...scores: number[]]
function report(...args: Params): string {
const [name, ...scores] = args
return name + ": " + scores.join(",")
}
console.log(report("Ada", 9, 8))Why Rest Tuples Matter
Rest tuples express shapes plain arrays cannot: a required header plus a variable body, a variable body plus a required footer, or both. They are the backbone of typing variadic functions.
type CSVRow = [id: number, ...fields: string[]]
const row: CSVRow = [1, "Ada", "Engineer"]
console.log(row[0], row.slice(1))Quick Check
Test your understanding of tuple rest elements.
Recap
A tuple rest element (...T[]) lets one position absorb any number of items, while fixed leading and trailing elements stay required. The rest may be trailing, leading, or in the middle, but only one rest is allowed per tuple. Destructuring aligns with these shapes, labels add readable hints, and rest tuples are the foundation for typing variadic functions.
Learn TypeScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 101
- Lessons
- 352
Frequently Asked Questions
Is the “Tuple Rest Elements” lesson free?
Yes — the full text of “Tuple Rest Elements” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO. The TypeScript Academy course includes 4 lessons in total.
What will I learn in “Tuple Rest Elements”?
Use rest elements inside tuple types. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Tuple Rest Elements” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Rest Parameters in Functions
- Spread in Arrays and Objects
- Tuple Rest Elements
- Typing Variadic Functions