TypeScript Best Practices
- Parse unknown external data at the boundary; keep
unknownuntil validated. - Prefer discriminated unions for mutually exclusive states and exhaustive handling for closed variants.
- Give domain meaning to primitives when accidental mixing would be costly.
- Avoid widening useful literal information and avoid
anyas an escape hatch. - Let callers consume one coherent return shape instead of correlated optional fields.
- Use generics only when they express a real relationship between inputs and outputs.
- Keep async/error states explicit at the public boundary.
- Test behavior through exported APIs, not private helper choreography.