TypeScript theorytheory 0/50 · 0%
Collections · easy

16. Records and Maps

Typed key-value structures.

`Record<K, V>` types a plain object used as a dictionary. `Map<K, V>` is the runtime collection class with guaranteed key order and any key type (not just strings).

ts
const balances: Record<string, number> = { "0xabc": 10, "0xdef": 5 };
balances["0xabc"] += 1;

const cache: Map<string, number> = new Map();
cache.set("0xabc", 10);
cache.get("0xabc"); // number | undefined

Prefer `Map` when keys aren't known ahead of time and you need reliable iteration order or non-string keys; prefer `Record`/plain objects for fixed, known shapes such as configuration maps or lookup tables serialized to JSON.

Check your understanding

  1. 1. What does `Record<string, number>` describe?

  2. 2. What type does `Map.get()` return?

  3. 3. What is an advantage of `Map` over a plain object?