TypeScript Type-Safety Rules
Review-oriented rule set for hunting type-system erosion in TypeScript codebases. 20 rules across 7 categories, prioritized by blast radius. Used as the audit checklist by the type-safety-auditor agent and usable standalone while writing or hardening TypeScript.
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Any Erosion | CRITICAL | any- |
| 2 | Unsound Casts | CRITICAL | cast- |
| 3 | Boundary Validation | CRITICAL | boundary- |
| 4 | Assertion Abuse | HIGH | assert- |
| 5 | Compiler Configuration | HIGH | config- |
| 6 | Exhaustiveness | MEDIUM-HIGH | exhaust- |
| 7 | Generics Soundness | MEDIUM | generics- |
Quick Reference
1. Any Erosion (CRITICAL)
any-explicit- Replace explicitanywithunknownplus narrowingany-implicit-boundary- Type the results ofJSON.parse,response.json(), andcatchimmediatelyany-generic-default- Never default a type parameter toany
2. Unsound Casts (CRITICAL)
cast-as-unsound- Anascast that changes the shape needs a runtime check insteadcast-double-as unknown as Xis a type-system bypass; write a guard or convertercast-const-assertion- Useas constfor literal narrowing instead of widening annotations
3. Boundary Validation (CRITICAL)
boundary-http- Parse HTTP payloads with a schema at the edgeboundary-queue- Validate queue and event messages before processingboundary-storage- Validate and version storage reads (localStorage, files, DB JSON)boundary-env- Access environment variables through one validated config module
4. Assertion Abuse (HIGH)
assert-non-null-!needs an adjacent invariant justification or a fail-fast checkassert-ts-expect-error- Use@ts-expect-errorwith a reason, never@ts-ignore
5. Compiler Configuration (HIGH)
config-strict-"strict": trueis the baselineconfig-unchecked-index- EnablenoUncheckedIndexedAccessconfig-exact-optional- EnableexactOptionalPropertyTypesconfig-skiplibcheck-skipLibCheckmay speed up third-party types, never hide first-party errors
6. Exhaustiveness (MEDIUM-HIGH)
exhaust-switch-never- Discriminated unions get aneverassertion in the default branchexhaust-satisfies-record- Usesatisfies Record<K, V>for complete lookup tables
7. Generics Soundness (MEDIUM)
generics-constraint- Constrain type parameters on public APIsgenerics-type-guards- A type predicate must verify the whole shape it claims
How to Use
Read individual rule files in the rules/ directory for detailed explanations and code examples. Each rule file contains a brief explanation, an incorrect example, a correct example, and a detection hint for reviewers.