Sub topic 7
When to Use a Tuple and When to Use an Array
Assume you know basic JavaScript arrays, TypeScript annotations, and union types. This chapter focuses on choosing between an array, which holds a variable-length sequence of similar values, and a tuple, which describes a fixed structure with a known type at each position.
Arrays Represent Collections
Use an array when the values play the same role and the length can vary.
const scores: number[] = [90, 85, 72];
const names: Array<string> = ["Ada", "Linus"];
scores.push(88);
names.push("Grace");The types number[] and Array<number> mean the same thing. The T[] form is shorter and common for simple types. The Array<T> form can be easier to read when the element type is complex.
An array is a good choice for:
- A list of users
- Search results
- Items in a shopping cart
- A sequence of measurements
- Any collection where position does not give a value a special meaning
The array literal below is inferred as number[] because all its elements are numbers:
const temperatures = [18, 21, 19];If the elements have different types, TypeScript usually infers a union array:
const values = [1, "pending", 2];
// (string | number)[]The array can contain any number of strings or numbers. It does not mean that the first element must be a number or that the second must be a string.
Tuples Represent Structure
Use a tuple when each position has a different meaning and the number of positions is known.
type Coordinate = [number, number];
const home: Coordinate = [51.5, -0.1];
const [latitude, longitude] = home;The first position is the latitude, and the second is the longitude. A tuple records that structure in the type.
type UserRecord = [id: number, username: string, active: boolean];
const user: UserRecord = [42, "ada", true];The labels document the meaning of each position. They do not create property names, so you still access values by index or destructure them.
| Requirement | Prefer |
|---|---|
| Variable length | Array |
| Elements share one role | Array |
| Each position has a distinct role | Tuple |
| Fixed number of values | Tuple |
| Many values processed in a loop | Array |
| Several return values with known positions | Tuple |
This diagram summarizes the decision:
Check the Meaning of Indexes
An array access usually means “give me the item at this position.” The position itself is not part of the data model.
function average(values: number[]): number {
if (values.length === 0) {
throw new Error("Cannot average an empty array");
}
return values.reduce((sum, value) => sum + value, 0) / values.length;
}A tuple access can have a specific meaning:
function splitName(name: string): [first: string, last: string] {
const [first, last] = name.split(" ");
return [first ?? "", last ?? ""];
}
const [firstName, lastName] = splitName("Ada Lovelace");If a tuple starts to contain many positions, or callers must remember what indexes mean, an object may communicate the model better:
type User = {
id: number;
username: string;
active: boolean;
};Exercise
Exercise
Should a function that returns a variable-length list of search results use an array or a tuple? What about a function that returns exactly a minimum and maximum value?
Show answer
Use an array for the search results because the length varies and the elements share one role. Use a tuple such as [number, number] for the minimum and maximum because the length and meaning of each position are fixed.