TypeScriptTips

Make bugs impossible.
One TypeScript tip at a time.

I like the tricks :) – Kasia

And types

Dry up code by extracting the common type.

type Alpha = { a: string, b: number, c: boolean, d: Date, w: W }
type Beta = { a: string, b: number, c: boolean, d: Date, z: Z }

const extractAFromAlpha = (alpha: Alpha): string => alpha.a
const extractAFromBeta = (beta: Beta): string => beta.a
// ⛔️ Looks suspiciously repeated
type Base = { a: string, b: number, c: boolean, d: Date }
type Alpha = Base & { w: W }
type Beta = Base & { z: Z }

const extractAFromBase =
  (base: Base): string => base.a // ✅ One function