TypeScriptTips

Make bugs impossible.
One TypeScript tip at a time.

I like the tricks :) – Kasia

Limit types

Avoid branching by limiting the number of types that flow in the program

type PostCode = string | number

const shipTo = (postCode: PostCode) => {
  if (typeof postCode === "string") {
    // format string
  } else { // ⛔️ Need to branch
    // format number
  }
}
type PostCode = string

const shipTo = (postCode: PostCode) => {
  // ✅ Only need to format string
}