TypeScript Rules

Rule

Type Definitions

  • MUSTUse interface over type for object type definitions.
  • MUSTUse type for unions, mapped types, and conditional types.
  • MUSTUse as const for literal type assertions.
  • MUSTUse import type and export type for type-only imports/exports.
  • NEVERUse unknown as a generic constraint (e.g., T extends unknown).

Type Safety

  • NEVERUse any. Prefer precise types or unknown with narrowing.
  • NEVERCast to any (as any, as unknown as). Fix types at the source.
  • NEVERUse unsafe as casts 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.