TypeScript - Arrays & Tuples

Arrays

const xs: number[] = [1, 2, 3];
const ys: Array = [1, 2, 3]; // generic syntax
const ro: readonly number[] = [1, 2, 3];

Tuples

type Point = [x: number, y: number];
const p: Point = [10, 20];

// Variadic tuples
function concat(...parts: T): T { return parts }
const t = concat([1], [2, 3] as const); // type: readonly [number[], readonly [2, 3]]

Common pitfalls

  • Mutating readonly arrays is not allowed.
  • Accessing tuple elements needs index checks if the tuple may vary in length.