"A tuple is an array where each slot has its own job."
What a tuple is
A tuple is a fixed-length array where each position can have its own type. [string, number] is an array of exactly two elements: the first is a string, the second is a number. The compiler tracks both the length and the per-position type.
Tuples are useful when the position carries meaning. Coordinate pairs ([x, y]), key-value pairs ([key, value]), the return-with-status pattern ([error, result]) — all of these are conceptually positional, and a tuple type lets the compiler enforce that.
Optional and rest elements in tuples
Tuples support modifiers similar to function parameters:
- Optional element:
[string, number?]— second element may be missing. - Rest element:
[string, ...number[]]— first element is a string, then any number of numbers. - Named tuple elements:
[name: string, age: number]— purely cosmetic labels that show up in IDE hover. They don't change behavior but help readability.
When tuples beat objects, and when they don't
Tuples shine when:
- The two-or-more values are commonly returned together and the positions have well-known meaning (React's
useStatereturns[value, setter]). - You want destructuring to match the shape directly:
const [x, y] = getCoords(). - The number of values is small (2–3) and the names would be obvious context.
Objects beat tuples when:
- There are 4+ values — humans lose track of position.
- The values are heterogeneous in role and a name would clarify (
{ user, role, permissions }vs[User, Role, Permission[]]). - The shape may grow over time — adding to a tuple breaks every destructuring call site.
Inference: arrays by default, tuples on request
Array literals widen to arrays by default. To get tuple types from a literal, use either explicit annotation or as const:
const a = [1, 'two']; // (string | number)[] — array of union
const b: [number, string] = [1, 'two']; // tuple, explicit
const c = [1, 'two'] as const; // readonly [1, 'two'] — tuple + literal
Pippa's confession
[ok, value] or [error, result]. Most data is object-shaped. Tuples are like literal types — narrow, sharp tools that win when the shape really is positional.