Back to TILs

TypeScript: satisfies operator

The satisfies operator (TS 4.9+) is a game changer. It lets you validate that an object matches a type without widening it.

type Colors = "red" | "green" | "blue";
type Theme = Record<Colors, string | number[]>;

const palette = {
  red: "#ff0000",
  green: [0, 255, 0],
  blue: "#0000ff",
} satisfies Theme;

// Now I can still use .toFixed() on green because TS knows it's an array!
palette.green.map((v) => v);

If I had used const palette: Theme = ..., palette.green would be string | number[], requiring type narrowing.