Tuple (TypeScript)

A tuple is an array with a fixed number of elements whose types are known at each position, like `[string, number]`. It lets you type the slots of an array individually.

TypeScript

A normal array type like `number[]` says every element is a number. A tuple instead fixes the length and the type of each position, which is perfect for pairs and small fixed records.

Tuples are what React's useState returns: a two-element tuple of the value and its setter function.

Syntax

let pair: [type1, type2];

Examples

TypeScript

let point: [number, number] = [3, 4];
let entry: [string, number] = ["age", 30];
console.log(point[0], entry[0]);

Output:

3 age

TypeScript — Compile error: Type 'number' is not assignable to type 'string'.

let pair: [string, number] = [1, "x"];

Learn it in a lesson

See also

Back to the Dictionary