What Tuples Are and When to Use Them
You'll learn when and why to use tuples—TypeScript's fixed-format arrays.
What Is a Tuple?
A tuple is an array with a fixed number of elements where each position has a specific, known type. Unlike regular arrays where all elements are the same type, tuples lock in both the length and the type at each index.
Think of a tuple like a form with specific fields: the first blank must be a name (string), the second must be an age (number), etc. You can't add extra blanks or swap the order.
When to Use Tuples
Tuples shine when you have related values that belong together but have different types:
Coordinates:
let point: [number, number] = [10, 25];
// x is always first (number), y is always second (number)
Key-value pairs:
let setting: [string, boolean] = ["darkMode", true];
// name is always first (string), value is always second (boolean)
Function returns with multiple values:
function getUser(): [string, number] {
return ["Alice", 30];
}
let user = getUser();
let name = user[0]; // TypeScript knows this is string
let age = user[1]; // TypeScript knows this is number
Why Not Just Use an Array?
With a regular array of unions, you lose position-specific type information:
let data: (string | number)[] = ["Alice", 30];
let first = data[0]; // Could be string OR number—TypeScript doesn't know!
With a tuple, TypeScript knows exactly what type lives at each position.
Key Takeaway: Tuples are fixed-length arrays with specific types at each index—use them when position matters and types differ, like coordinates, pairs, or structured multi-value returns.