TExcelTAD
Home › TypeScript › TS Tuples

TypeScript Tuples

A tuple is an array with a fixed length where each position has its own type. Where number[] means "any number of numbers", the tuple [string, number] means "exactly two items: first a string, then a number".

Tuples are handy for small groups of values that belong together but don't deserve a full object: a coordinate, a key/value pair, or a function that returns two results.

Defining a tuple

ts · try it yourself
let employee: [string, number, boolean] = ["Lan", 34, true];

const [who, age, active] = employee;
console.log(who.toUpperCase(), age + 1, active);
console.log(employee.length);
LAN 35 true
3

Each position is checked separately, so order matters:

ts · try it yourself
let employee: [string, number, boolean];
employee = [34, "Lan", true];
main.ts(2,13): error TS2322: Type 'number' is not assignable to type 'string'.
main.ts(2,17): error TS2322: Type 'string' is not assignable to type 'number'.

The length is checked too. Reading a position that doesn't exist is a compile-time error rather than a silent undefined:

ts · try it yourself
const pair: [string, number] = ["width", 640];
console.log(pair[2]);
main.ts(2,18): error TS2493: Tuple type '[string, number]' of length '2' has no element at index '2'.

Named tuple elements

You can give each position a label. Labels don't change the type; they make it self-documenting and appear in editor hints:

ts · try it yourself
type Span = [start: number, end: number];

function spanLength(span: Span): number {
  const [start, end] = span;
  return end - start;
}

console.log(spanLength([10, 25]));
15

Optional and rest elements

A ? marks an optional element at the end. A ... rest element allows any number of items of one type:

ts · try it yourself
type Rgb = [red: number, green: number, blue: number, alpha?: number];
type Command = [verb: string, ...args: string[]];

const solid: Rgb = [255, 128, 0];
const faded: Rgb = [255, 128, 0, 0.5];

const cmd: Command = ["copy", "a.txt", "b.txt"];
const [verb, ...files] = cmd;

console.log(solid.length, faded[3] ?? 1);
console.log(verb, files);
3 0.5
copy [ 'a.txt', 'b.txt' ]

Returning several values

Tuples shine as return types. React's useState hook is the best-known example: it returns a [value, setter] pair that you destructure with your own names.

ts · try it yourself
function minMax(values: number[]): [min: number, max: number] {
  return [Math.min(...values), Math.max(...values)];
}

const [low, high] = minMax([7, 2, 9, 4]);
console.log(`range ${low}..${high}`);
range 2..9

Readonly tuples

A plain tuple is still a JavaScript array, so push is technically allowed and would break the fixed length. Make it readonly to prevent any mutation:

ts · try it yourself
const corner: readonly [number, number] = [0, 0];
corner[0] = 5;
main.ts(2,8): error TS2540: Cannot assign to '0' because it is a read-only property.

The as const assertion produces a readonly tuple automatically, with literal types for each element:

ts
const size = [1920, 1080] as const;   // readonly [1920, 1080]

Tip: If a tuple grows beyond 3–4 elements, switch to an object with named properties. user.email is far easier to read than user[3].

Tuples vs arrays vs objects

Use When
T[] a list of any length where all items are the same kind
[A, B] a short, fixed group of values where position has meaning
{ a: A; b: B } several values that are best identified by name

Test yourself

Which value is valid for the type [number, string]?

  1. ["a", 1]
  2. [1, "a"]
  3. [1, "a", 2]
  4. [1]

How do you mark the last element of a tuple as optional?

  1. [number, string | undefined?]
  2. [number, ?string]
  3. [number, string?]
  4. [number, optional string]

What does [1, 2] as const produce?

  1. number[]
  2. [number, number]
  3. readonly [1, 2]
  4. const number[]