Skip to content

Tuples

Fixed-length arrays where each position has a specific type. Good for things like coordinates or RGB values.

let rgb: [number, number, number];

rgb = [255, 128, 0]; // valid
rgb = [255, 128]; // Error: needs exactly 3 elements
rgb = ["255", 128, 0]; // Error: first element must be number
const point: readonly [number, number] = [10, 20];

point[0] = 5; // Error: cannot modify readonly tuple
point.push(30); // Error: push doesn't exist on readonly

Labels make it clearer what each position means:

const coords: [lat: number, lng: number] = [40.7128, -74.006];
const [lat, lng] = coords;
console.log(lat); // 40.7128