TypeScriptTips

Make bugs impossible.
One TypeScript tip at a time.

I like the tricks :) – Kasia

Pick instead of duplicating

You can use Pick to avoid duplication and keep in sync two types.

type Post = {
  author: string,
  title: string,
  body: string,
}

type PostPreview = {
  title: string,
  body: string,
} // ⛔️ Need to keep in sync with Post
type Post = {
  author: string,
  title: string,
  body: string,
}

type PostPreview = Pick<Post, 'title'|'body'>
type PostPreview =
  Pick<Post, 'hah'> // ✅ Does not compile