typescript-best-practices
Installation
SKILL.md
TypeScript best practices
Make the compiler reject impossible states, mixed-up ids and unhandled variants. In a FiveM resource most of the type safety leaks out at the runtime's edges: onNet, exports, state bags and NUI callbacks are typed any or Function, so a type annotation there is a cast that nothing checks. Parse at those edges and trust the types everywhere else.
Principles
- Make illegal states unrepresentable. If you can write a comment explaining when a combination of fields is valid, split it into a discriminated union.
- Build types, don't restrict them. Choose the shape that can't hold the illegal value (a head plus a rest, a start plus a duration) instead of a loose type plus runtime checks.
- Don't lie to the compiler. If it can't prove a fact, prove it (validate, narrow, remodel). Trace every
any,asand!to the boundary and validate there. - Strengthen a type only where partiality appears. A
!, a cast or a "should never happen" throw marks a type that is too weak. Push that check into the type, then stop. - Validate at boundaries, trust inside. Business logic lives in plain functions that take typed data. The event handler around it is thin: parse, call, reply.