Arrays & Tuples
8 min read
Arrays & Tuples
An array type is written as Type[] or Array<Type>. Both forms are equivalent; Type[] is more common in everyday code.
ts
const names: string[] = ["Alice", "Bob", "Carol"];
const scores: Array<number> = [98, 87, 76];
// TypeScript catches wrong element types:
names.push(42); // Error: Argument of type 'number' is not assignable to type 'string'Tuples
A tuple is a fixed-length array where each position has a known type. Tuples are useful for returning multiple values from a function without a named object.
ts
// Tuple: [string, number]
const user: [string, number] = ["Alice", 30];
const [name, userAge] = user; // destructuring works
// Named tuple elements (TypeScript 4+)
const point: [x: number, y: number] = [10, 20];
// Readonly tuple
const rgb: readonly [number, number, number] = [255, 128, 0];
rgb[0] = 0; // Error — tuple is readonlyTuples are structurally compatible with arrays of their element types. TypeScript does not enforce tuple length at runtime — that is a compile-time-only guarantee.
Ready to test yourself?
Practice TypeScript— quiz & coding exercises