Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Arrays & Tuples · Lesson 8 of 12

Tuples

A free lesson from Swipe Coding · TypeScript, level 2 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
Fixed slots, fixed types — tuples are arrays with much stricter rules. 📌

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
The tuple type [string, number] describes exactly two slots: a string first, then a number. Not "an array of strings and numbers" — a specific pair in a specific order.

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'.
Both the initial assignment and any later reassignment are checked against the position's declared type — position 0 is locked to string for the life of this tuple.
🤖
Order matters here — swap two values and you've got a completely different meaning. 🔀

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: string
Destructuring a tuple hands each variable the precise type from its position — result is known to be a number and status a string, not "number or string."
Fill in the blank

Unlike a regular array, a tuple has a fixed ____ and a specific type for each position.

Quick check

Given let entry: [string, number] = ["score", 100];, what is the type of entry[1]?

True or false

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 →