Sub topic 12
Tuple Pitfalls: `push` and Out-of-Range Access
Assume you know JavaScript arrays, basic TypeScript annotations, and union types. This chapter focuses on two surprising behaviors: mutable tuples can grow with push, and tuple indexing depends on whether the index is known at compile time.
A Tuple Is an Array With Extra Information
A tuple records both the length and the type at each position:
type UserEntry = [id: number, name: string];
const entry: UserEntry = [42, "Ada"];
const id = entry[0]; // number
const name = entry[1]; // string
// const invalid = entry[2]; // Error: no element at index 2TypeScript knows that entry[0] is a number and entry[1] is a string. It also knows that this tuple has exactly two declared positions.
A normal array describes the element type but not a fixed length:
const values: number[] = [10, 20];
const first = values[0]; // number, or number | undefined with a strict setting
const later = values[100]; // allowed by the type systemThe array type allows any numeric index because arrays can grow or shrink.
Why `push` Can Break a Mutable Tuple
A tuple without readonly is still a mutable array. TypeScript checks the type accepted by push, but it does not use the tuple's fixed length to reject the operation.
const pair: [number, string] = [1, "one"];
pair.push(2);
pair.push("two");
// pair is still declared as [number, string]
// pair[2]; // Error: Tuple type '[number, string]' has no element at index 2The tuple now contains four runtime elements, but its static type still describes only the first two positions. This creates a mismatch between the value at runtime and the type known to the compiler.
The value passed to push must be assignable to the tuple's element types. Therefore, both a number and a string are accepted in this example. A boolean is rejected:
const pair: [number, string] = [1, "one"];
// pair.push(true); // Error: boolean is not assignable to number | stringThe type check protects element types, but not the tuple's length.
Preventing Tuple Mutation
Use a readonly tuple when the number and position of elements must remain stable:
const pair: readonly [number, string] = [1, "one"];
// pair.push(2); // Error: push does not exist on a readonly tuple
// pair[0] = 2; // Error: the index signature is readonlyreadonly prevents mutation through that reference. It does not make the individual values deeply immutable if a tuple contains objects.
Out-of-Range Access
A numeric literal index lets TypeScript detect an invalid tuple position:
type Point = [number, number];
const point: Point = [10, 20];
const x = point[0]; // number
const y = point[1]; // number
// const z = point[2]; // Error: no element at index 2An index stored in a variable is different:
const index: number = Math.random() > 0.5 ? 0 : 1;
const coordinate = point[index]; // numberHere, the compiler cannot know which valid position will be used. If the index could be outside the tuple, strict checking may reject the access or produce a type that includes undefined.
For ordinary arrays, enable noUncheckedIndexedAccess when missing elements must be handled explicitly:
const scores: number[] = [90, 85];
const score = scores[10]; // number | undefined with noUncheckedIndexedAccess
if (score !== undefined) {
console.log(score.toFixed(0));
}This setting reflects runtime behavior: an invalid array index returns undefined.
Choosing the Safe Form
| Situation | Suitable type | Reason |
|---|---|---|
| A fixed pair that must not change | `readonly [number, number]` | Preserves positions and blocks mutation |
| A growing collection of numbers | `number[]` | Length is intentionally variable |
| A fixed pair that changes during construction | `[number, string]` | Mutation is allowed, but length needs manual care |
Exercise
What is the safest type for a two-number coordinate that should never be changed through the current reference?
Show answer
Use readonly [number, number].