TypeScript Rules
Rule
Type Definitions
- MUSTUse
interfaceovertypefor object type definitions. - MUSTUse
typefor unions, mapped types, and conditional types. - MUSTUse
as constfor literal type assertions. - MUSTUse
import typeandexport typefor type-only imports/exports. - NEVERUse
unknownas a generic constraint (e.g.,T extends unknown).
Type Safety
- NEVERUse
any. Prefer precise types orunknownwith narrowing. - NEVERCast to
any(as any,as unknown as). Fix types at the source. - NEVERUse unsafe
ascasts to bypass errors. Wrong types = wrong code. - NEVERUse non-null assertions (
!). Fix types at source.
Error Handling
- NEVERAdd unnecessary
try/catch. Let errors propagate. - SHOULDOnly catch when you can recover, transform, or report.
Simplicity
- SHOULDOnly create abstractions when actually needed. Inline is fine.
- SHOULDAvoid helper functions when a simple inline expression suffices.