TypeScript - Maps, Sets & Records

Map and Set

const m = new Map();
m.set('a', 1);
const s = new Set([1,2,3]);

Record

type Status = 'idle' | 'loading' | 'done';
const labels: Record = {
  idle: 'Idle', loading: 'Loading', done: 'Done'
};

Index signatures vs Map

  • Record/index signature: great when keys are known (finite union or string map).
  • Map: best for dynamic keys, non-string keys, or need iteration order/size.