Arrays & Tuples
Learn how to type arrays of a single type, and fixed-length, fixed-type tuples.
Reading Time
14 min
Lesson
Lesson 4 of 30
Typing Arrays
An array type can be written as Type[] or the equivalent generic form Array<Type>. Both mean exactly the same thing.
Two Equivalent Syntaxes
let ids: number[] = [1, 2, 3];
let names: Array<string> = ["Alice", "Bob"];Arrays of Objects
Array element types aren’t limited to primitives — they can be object shapes, interfaces, or unions.
Array of Objects
interface User {
id: number;
name: string;
}
const users: User[] = [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
];What is a Tuple?
A tuple is a fixed-length array where each position has its own specific type — unlike a regular array, order and count matter.
Basic Tuple
let point: [number, number] = [10, 20];
let entry: [string, number] = ["age", 25];
// entry[0] is always a string, entry[1] is always a numberTuples Enforce Position and Length
let coordinate: [number, number];
coordinate = [10, 20]; // OK
coordinate = [10, 20, 30]; // Error: too many elements
coordinate = ["10", 20]; // Error: wrong type at index 0Named Tuple Members
Tuple elements can be labeled for clarity in editor tooltips — the labels are purely documentation and don’t change runtime behavior.
Labeled Tuple
let rgb: [red: number, green: number, blue: number] = [255, 0, 128];Optional and Rest Elements in Tuples
Tuples support optional elements with ? and a rest element to allow a variable number of trailing items of a given type.
Optional and Rest Tuple Elements
let range: [start: number, end?: number] = [0];
let scores: [name: string, ...results: number[]] = ["Alice", 90, 85, 100];When to Reach for a Tuple
Tuples are ideal for small, fixed structures like a coordinate pair or a React useState() return value — [value, setValue] — where position carries specific meaning.
Best Practice
Prefer a regular array (Type[]) for lists of similar items, and reach for a tuple only when the position of each element has a distinct, fixed meaning.