← All languages
TypeScript construct of the day
Random

tuple types

Fixed-length arrays where each element has a specific type.

Description

A tuple type is an array type with a known length and a specific type for each position. For example, [string, number] describes an array whose first element is a string and whose second element is a number. TypeScript enforces both the length and the element types when reading from or writing to a tuple.

Tuples can include optional elements (marked with a trailing ?), rest elements (...T[]) for variable-length sections, labeled elements such as [name: string, age: number] for clearer documentation, and the readonly modifier to prevent mutation. These features make tuples a powerful way to model heterogeneous fixed-shape data without defining an interface.

A particularly important use of tuples is typing the parameter list of a function: TypeScript represents Parameters<T> and rest spreads as tuples internally. Variadic tuple types let you build higher-order utilities that manipulate parameter lists generically, which is the foundation of many modern type-level libraries.

Example

type Coordinate = [x: number, y: number];

const origin: Coordinate = [0, 0];
const [x, y] = origin;

// Rest tuple
type StringsAndNumber = [...string[], number];
const v: StringsAndNumber = ['a', 'b', 42];

Reference