TypeScriptTips

Make bugs impossible.
One TypeScript tip at a time.

I like the tricks :) – Kasia

Check authorization with types

Moving authorization from value to type level can make your code simpler and bullet-proof.

const dropDatabase = (performedBy: User) => {
  if (performedBy.type === 'MEBMER') { // ⛔️ Typo
    return
  }
  // ...
}

dropDatabase(member) // ⛔️
type User = Admin | Member

const dropDatabase = (performedBy: Admin) => {
  // ...
}

dropDatabase(member) // ✅ Does not compile