Arrays and tuples#
Homogeneous array.
const xs: number[] = [1, 2, 3];
Generic-style spelling; identical.
const ys: Array<number> = [1, 2, 3];
Read-only array (no mutator methods).
const rs: readonly number[] = [1, 2, 3];
Fixed-length tuple.
const pair: [string, number] = ["rk", 30];
Labelled tuple positions.
const labelled: [name: string, age: number] = ["rk", 30];
Variadic tuple (head plus rest).
const variadic: [string, ...number[]] = ["x", 1, 2];
A tuple gives positional names and a fixed length; an array is homogeneous and variable-length.
References#
Object types for the related object-shape form.
Literal types for
as const(which turns arrays into readonly tuples).