Sub topic 11
Readonly tuples and `as const`
Prerequisites: you should know basic TypeScript annotations, union types, arrays, and ordinary tuples. This chapter focuses on preserving exact tuple positions while preventing mutation through a particular reference.
A tuple records both the number of elements and the type at each position:
type Point = [number, number];
const point: Point = [10, 20];A readonly tuple has the same positional information, but its elements cannot be changed through that reference:
type ReadonlyPoint = readonly [number, number];
const origin: ReadonlyPoint = [0, 0];
// origin[0] = 5; // Error: cannot assign to a read-only property
// origin.push(1); // Error: push does not existreadonly protects the reference used to access the tuple. It does not freeze the array at runtime.
`as const` creates a readonly tuple
An array literal normally widens to an array type. That means TypeScript keeps the general element types but forgets the exact length and positions:
const pair = [1, "one"];
// pair: (string | number)[]The as const assertion prevents this widening. For an array literal, it produces a readonly tuple with literal element types:
const fixedPair = [1, "one"] as const;
// readonly [1, "one"]
const [count, label] = fixedPair;
// count: 1
// label: "one"The const declaration prevents reassignment of the variable. The as const assertion also preserves literal values and makes the array readonly. These are different protections.
let a = [1, 2] as const;
// a = [3, 4]; // Error: a cannot be assigned a new value
// a[0] = 3; // Error: the tuple is readonly
const b = [1, 2];
// b = [3, 4]; // Error: b cannot be reassigned
b[0] = 3; // Allowed: b is still a mutable number[]Assignability and function parameters
A mutable tuple can be passed where a readonly tuple is expected. The function promises not to mutate its input, so accepting a mutable caller value is safe:
function distanceFromOrigin(point: readonly [number, number]): number {
const [x, y] = point;
return Math.sqrt(x * x + y * y);
}
const mutablePoint: [number, number] = [3, 4];
const fixedPoint = [6, 8] as const;
console.log(distanceFromOrigin(mutablePoint)); // 5
console.log(distanceFromOrigin(fixedPoint)); // 10The reverse assignment is rejected. A readonly tuple cannot be used as a mutable tuple because code receiving the mutable type could change it.
const readonlyPoint = [3, 4] as const;
// const mutable: [number, number] = readonlyPoint; // ErrorUse readonly tuple parameters when a function only reads its arguments. This makes the function work with both mutable and readonly callers.
A useful mental model
Choose as const for fixed data such as coordinate pairs, command descriptions, or lookup entries where both the values and positions matter. Use an ordinary mutable tuple when callers must update its elements. Use a readonly array when the length and positions are not part of the contract.
const command = ["resize", 800, 600] as const;
// readonly ["resize", 800, 600]
function describeCommand(value: readonly [string, number, number]): string {
return `${value[0]} to ${value[1]}x${value[2]}`;
}
console.log(describeCommand(command));Exercise
What is the type of const value = ["ready", 200] as const, and can value[1] be assigned a new number?
Show answer
Its type is readonly ["ready", 200]. No. The tuple is readonly, and its second element has the literal type 200.