TypeScriptTips

Make bugs impossible.
One TypeScript tip at a time.

I like the tricks :) – Kasia

Enumerating a union type

Avoid enumerating all the values of a union type manually.

type Check = 'Avionics' | 'Recovery' | 'Navigation'
const checklist: Check[] =
  ['Avionics', 'Recovery'] // ⛔️ Forgot 'Navigation'
const checklist = // ✅ Single point of truth
  ['Avionics', 'Recovery', 'Navigation'] as const
type Checklist = (typeof checklist)
//   ^? type Checklist = readonly ['Avionics', 'Recovery', 'Navigation']
type Check = Checklist[number]
//   ^? type Check = 'Avionics' | 'Recovery' | 'Navigation'