TypeScriptTips

Make bugs impossible.
One TypeScript tip at a time.

I like the tricks :) – Kasia

Extract union types from record types

Let the compiler keep in sync your types, so you don't have to remember it yourself.

type Admin = { _kind: 'admin' }
type Member = { _kind: 'member' }
type Visitor = { _kind: 'visitor' }

type Roles = 'admin' | 'member' // ⛔️ Forgot 'visitor'
type User = Admin | Member | Visitor

type Roles = User['_kind']
//   ^? type Roles = 'admin' | 'member' | 'visitor'