typescript
TypeScript style
Prerequisite: load the javascript skill first. Every rule in javascript (early returns over else, const over let, no chained ternaries, brace all control-flow bodies, no .forEach) applies to TypeScript too. This skill only adds the TS-specific rules layered on top — types, generics, enums, narrowing.
Personal TS-specific style rules to follow whenever touching TypeScript. Two themes underlie everything:
- Don't lie to the compiler. Casts,
any,!, and@ts-ignoreall silently disable type checking. If you're wrong, nothing tells you — until production. Prefer constructs that prove the claim (type guards,satisfies, narrowing). - Let the type system do the work. Generics, overloads, utility types, and discriminated unions let TypeScript infer the exact shape at every call site. Stop hand-typing what the compiler can derive.
Both themes catch real bugs that show up when code is refactored or reused — exactly the situations where untyped escape hatches rot fastest.
Part 1 — Don't lie to the compiler
1.1 No as casting — use type guards
as X and especially as unknown as X silently tell the compiler "trust me." A type guard does the same narrowing work and checks reality at runtime, so the call site is honest about what it actually knows.