TypeScriptTips

Make bugs impossible.
One TypeScript tip at a time.

I like the tricks :) – Kasia

Keep in sync with conditional types

You can create types from types to make sure they remain in sync.

type Role = 'ADMIN' | 'MEMBER' | 'VISITOR'
type Unprivileged = 'MEMBER' | 'VISITOR'

// ⛔️ Need to add unprivileged roles to both
type AdminRole = 'ADMIN'
type Role = AdminRole | 'MEMBER' | 'VISITOR'

type RemoveAdmin<R extends Role> =
  R extends AdminRole ? never : R

type Unprivileged = RemoveAdmin<Role>