Fixed-Length, Fixed-Type Arrays
Tuples
Tuples are arrays with a known length and a specific type at each index. Great for fixed-shape pairs, like `[name, age]`.
What you'll learn
- Author a tuple type
- Use optional and rest elements
- Recognize when a tuple beats an object
A tuple is an array with a fixed length where each position has its own type.
The Syntax
let pair: [string, number] = ["Ada", 36];
pair[0]; // string
pair[1]; // number
pair[2]; // ✗ Tuple type '[string, number]' has no element at index '2'. You set the length and types at each position. Beyond that, TS complains.
Named Tuple Elements
For readability, name the positions:
let user: [name: string, age: number] = ["Ada", 36]; The names are documentation only — the values are still accessed by index.
Common Use Cases
Return multiple values
function minMax(nums: number[]): [min: number, max: number] {
return [Math.min(...nums), Math.max(...nums)];
}
const [min, max] = minMax([3, 1, 4]); React-style state hook returns
function useToggle(): [boolean, () => void] {
let on = false;
const toggle = () => { on = !on; };
return [on, toggle];
} This is why const [count, setCount] = useState(0) works the way
it does — useState returns a tuple.
Optional Elements
type Coord = [x: number, y: number, z?: number];
const a: Coord = [1, 2];
const b: Coord = [1, 2, 3]; Rest Elements
type Header = [name: string, ...values: string[]];
const h: Header = ["Authorization", "Bearer", "abc123"]; readonly Tuples
const pair: readonly [string, number] = ["Ada", 36];
pair[0] = "Grace";
// ~~~~~ Cannot assign to '0' because it is a read-only property. Tuple vs Object — Which?
| When | Use |
|---|---|
| Pair / triple where order is the meaning | Tuple |
| Many fields, named for clarity | Object |
| Multiple-return where caller destructures | Tuple |
| Anything you’d describe with names (“user”) | Object |
Tuples shine for short, ordered returns. Objects shine when fields have semantic names.
Up Next
Objects — TypeScript’s everyday workhorse.
Objects →