TypeScriptTips

Make bugs impossible.
One TypeScript tip at a time.

I like the tricks :) – Kasia

const Type Parameters

Make TypeScript v5+ infer the more specific type (instead of the most generic).

const first = (xs: unknown[]) => xs[0]
pick(["a", "b"])
// ^? unknown

const first = (xs: any[]) => xs[0]
pick(["a", "b"])
// ^? any

const first = <T>(xs: T[]) => xs[0]
pick(["a", "b"])
// ^? string
const pick = <const T>(xs: T[]) => xs[0]

pick(["a", "b"])
// ^? "a" | "b"