TypeScript Tips

Make bugs impossible.
One TypeScript tip at a time.

I like the tricks :) – Kasia

Extract instead of duplicating

@RiccardoOdone Suggest a tip

Use Extract to derive a type from another one and maintain a single point-of-truth.

type Keys = "commentId" | "userId" | "rating"
type Refs = "commentId" | "userId"
// ⛔️ Need to keep Keys and Refs in sync
type Keys = "commentId" | "userId" | "rating"
type Refs = Extract<Keys, `${string}Id`>
//   ^? "commentId" | "userId"