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