Tuples
A free lesson from Swipe Coding · TypeScript, level 2 · updated October 2026
By the Swipe team, founded by Colin · how we make lessons
A regular array like number[] can be any length, and every element shares the same type. A tuple is different: it's a fixed-length array where each position has its own, specific type. TypeScript checks both the length and the type at every position.
let point: [number, number] = [10, 20]; let user: [string, number] = ["Ada", 36]; // position 0 must be a string, position 1 must be a number
That ordering is the whole point of a tuple. Swap the values and TypeScript catches it immediately, the same way it would catch assigning a number where a string was expected. A plain string[] or number[] gives you no such guarantee about what lives at each index.
let user: [string, number] = [36, "Ada"]; // Error: Type 'number' is not assignable to type 'string'. user[0] = 42; // Error: Type 'number' is not assignable to type 'string'.
Tuples are a great fit whenever a fixed handful of related values travel together and their order carries meaning — a coordinate pair, an [error, result] pair from a function, or a [key, value] entry. Reading a tuple element gives you back the exact type declared for that position, not a generic union of every possible type in the tuple.
function divide(a: number, b: number): [number, string] {
if (b === 0) return [0, "cannot divide by zero"];
return [a / b, "ok"];
}
const [result, status] = divide(10, 2);
// result: number, status: stringUnlike a regular array, a tuple has a fixed ____ and a specific type for each position.
Given let entry: [string, number] = ["score", 100];, what is the type of entry[1]?
let pair: [string, number] = ["a", 1, 2]; compiles without error because arrays can hold extra elements.
Keep your streak going in Swipe Coding
Swipe through every level of TypeScript on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.
Get the Swipe apps →