TypeScript Cheatsheet
Power user.
Overview
TypeScript is the typed superset of JavaScript that backs most modern frontend and Node.js work. Five surfaces cover almost every operational use: static type checking, type inference, generics for reusable abstractions, discriminated unions for exhaustive pattern matching, and the type-level toolkit (mapped, conditional, template literal types) for library-grade type safety.
- Static type checking. Compile-time type errors. Catches whole classes of bugs that JavaScript would only surface at 3am.
- Type inference. The compiler infers types from usage. Most of the file does not need explicit annotations.
- Generics. Reusable type-parameterised code. Type-safe abstractions without sacrificing inference.
- Discriminated unions plus type-level programming. Sum types via string literal kinds; mapped and conditional types for library-grade type safety.
The approach
Five patterns carry most of the day-to-day operational weight. Memorising them moves a team from “TypeScript adds friction” to “TypeScript catches our bugs at compile time”.
"strict": true. Enable strict mode. The single biggest source of bugs caught at compile time.typeversusinterfaceas convention. Both work. Pick one and stop debating it in code reviews.- Discriminated unions.
{ kind: "a"; ... } | { kind: "b"; ... }with exhaustive switches. The compiler enforces all cases handled. as constplussatisfies. Literal-type assertions for narrow types;satisfiesvalidates without widening.
Why this compounds
Each typed module deposits intent the team can read off the type signatures. Refactoring becomes safer; cross-team collaboration improves because the contract lives in code; new engineers onboard faster because the types are documentation.
- Compile-time safety. Bugs caught at
tscnever reach production. Runtime bug count drops measurably. - Refactoring becomes routine. Type-aware tooling supports rename, extract, and signature changes safely.
- Collaboration improves. Types document interfaces between teams. Slack threads about “what does this return” disappear.
- Year-one investment, year-two habit. First year is fluency-building. By year two, the team writes idiomatic TypeScript without thinking.