TypeScript Tricks I Wish I Knew Earlier

TypeScript Tricks I Wish I Knew Earlier

Discriminated unions, template literals, satisfies operator, and more. Level up your TS game.


TypeScript is more powerful than most developers realize. Here are patterns that leveled up my code.

Discriminated Unions

Perfect for state machines and API responses. The compiler narrows types automatically.

Template Literal Types

Generate string types dynamically. Great for event names, CSS classes, and route patterns.

The satisfies Operator

Validate types without widening. You get autocomplete AND type safety.

Const Assertions

Use as const to preserve literal types in arrays and objects. Essential for configuration objects.

Tips

  • Use unknown over any — it forces you to narrow
  • Leverage utility types: Pick, Omit, Partial, Required
  • Enable strict mode from day one — it catches bugs early