Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
TypeScript Types  ›  Lesson

Arrays and Tuples

Array 12 minAn array holds any number of values of one type; a tuple holds a fixed number of values where each position has its own type.

Arrays and Tuples

A project holds a list of task titles, and it could be three titles or three hundred. Elsewhere you pass around a pair — a task's id and its title. Both are JavaScript arrays at runtime, but TypeScript describes them differently, because they mean different things.

The rule

Array: any length, one element type. Tuple: fixed length, a type per position.

Arrays have two equivalent spellings:

let titles: string[] = ["Buy milk", "Call Sam"];
let ids: Array<number> = [1, 2, 3];

titles.push("Pay rent");   // fine
titles.push(42);           // error: 42 is not a string
const first = titles[0];   // string

Every element shares the one element type, and the length can change freely. Want an array nobody can modify? Use readonly string[] (or ReadonlyArray<string>), and methods like push disappear from the type.

Tuples fix the number of elements and give each position its own type:

let entry: [number, string] = [7, "Buy milk"];

entry[0];          // number
entry[1];          // string
entry = ["Buy milk", 7];   // error: the positions are swapped
entry[2];                  // error: the tuple has no index 2

Tuples can also name their positions for readability, [id: number, title: string], and mark trailing positions optional with ?, as in [number, string, boolean?]. A well-known example is React's useState, which returns a tuple: the current value and a function to change it.

Remember that inference produces arrays, not tuples: let pair = [7, "Buy milk"] is inferred as an array whose elements are each a number or a string. If you want a tuple, annotate it (or use as const, covered later).

The shortcut

Ask "Does each position mean something different?"

  • No, they are all the same kind of thing (titles, ids, tasks) → array: T[].
  • Yes, position 0 is one thing and position 1 another → tuple: [A, B].
  • More than about three positions → use an object with named properties instead.
Choosing types in the task trackerStep 1 of 4
  1. All the tasks in a project

    Every element is a Task and the count varies, so Task[].

The trap is using a tuple where an object would be clearer. row[3] tells a reader nothing; row.due tells them everything.

Drills

Answer fast — trust the rule.

quick drill

Which two annotations mean the same thing?

quick drill

What is the type of pair[1] if pair: [number, string]?

quick drill

Which value fits the type [number, string]?

quick drill

How do you type a list of any number of id-title pairs?

Remember

  • Array: any length, one element type. Tuple: fixed length, a type per position.
  • Ask "does each position mean something different?" — yes means tuple.
  • Trap: long tuples are hard to read; switch to an object with named fields.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainList or pair?

You need to store (a) all the task titles in a project and (b) a single task's id together with its title, as in [7, "Buy milk"]. Explain which is an array and which is a tuple, and write both types.