TypeScriptTips

Make bugs impossible.
One TypeScript tip at a time.

I like the tricks :) – Kasia

Partial considered harmful

If you want to avoid undefined, Pick may be a better option.

type Options = { a: string, b: string }
const defaults = { a: 'a', b: 'b' }

const update = (override: Partial<Options>): Options =>
  ({ ...defaults, ...override })

update({ a: undefined })
// ⛔️ { "a": undefined, "b": "b" }
const update = <K extends keyof Options>
  (override: Pick<Options, K>): Options =>
    ({ ...defaults, ...override })

update({ a: undefined }) // ✅ Does not compile