What is a tuple, and how does it differ from a regular array type?
Short answer
A tuple is a fixed-LENGTH array where each position has its own SPECIFIC type, unlike a regular array type where every element must share the same single type — tuples are declared with square brackets listing each position's type in order.
Advertisement
AModel answer
A tuple is a fixed-LENGTH array where each position has its own SPECIFIC type, unlike a regular array type where every element must share the same single type — tuples are declared with square brackets listing each position's type in order.
let point: [number, number] = [3, 4]; // exactly two numbers
let entry: [string, number] = ['age', 30]; // first is string, second is number
// let bad: [number, number] = [1, 2, 3]; // Error: too many elementsReal-world example
Typing a coordinate pair, a key-value entry, or a fixed-shape function return like [value, error].
Likely follow-ups
- How does destructuring a tuple differ from destructuring a regular array in terms of type safety?
Related topics