TypeScript theorytheory 0/50 · 0%
Types · easy

3. Arrays and tuples

Typed lists and fixed-length, fixed-type tuples.

Arrays are written as `T[]` or `Array<T>`. Every element must match the declared type, and the compiler checks methods like `.push()` and `.map()` accordingly.

ts
const gasPrices: number[] = [12, 15, 9];
const names: Array<string> = ["Alice", "Bob"];

Tuples are fixed-length arrays where each position has its own type — perfect for things like `[address, amount]` pairs.

ts
type Transfer = [string, number];
const t: Transfer = ["0xabc", 100];

Tuples can also have named elements and optional/rest positions, e.g. `[to: string, amount: number, memo?: string]`, which documents intent while keeping the same underlying array shape.

Check your understanding

  1. 1. How do you write the type for an array of strings?

  2. 2. What distinguishes a tuple from a regular array type?

  3. 3. What does `[to: string, amount: number, memo?: string]` express?