SuperAPI AI Directory

typescript-strict

Coding & development

View repository

Updated 10-15 20:32

About this skill

Strict TypeScript

Types must model the domain and fail early. Compile-time does not replace validation at the boundary. Form UX: better-forms.

Rules

  • Treat strict as the baseline. Do not turn off flags to “pass the build”.
  • Forbid new any . Prefer unknown + narrowing.
  • Avoid blind as ; use type guards, predicates, and schemas.
  • Model nullability explicitly (T | null), not with magic sentinels.
  • Keep domain types in dedicated modules; do not spread duplicate interfaces.

Modeling

  • Use union types and discriminated unions for states.
  • Prefer Readonly / as const for immutable data.
  • Generics: constrain with extends; avoid decorative generics.
  • Extract types from values (typeof, ReturnType, Awaited) instead of duplicating.
  • For external APIs, define request/response types and equivalent Zod/Valibot schemas.

Runtime

  • Validate at the boundary: HTTP, forms, env, webhooks, localStorage.
  • Inference: z.infer<typeof Schema> as a single source.
  • Typed errors: result types or discriminable error classes.
  • Env: parse with a schema at boot; fail if invalid.

Safe refactoring

  1. Introduce types at the boundary.
  2. Push unknown inward until narrowing.
  3. Remove casts gradually.
  4. Run tsc --noEmit / project typecheck after changes.

Anti-patterns

  • any, as any, @ts-ignore without a short, local justification
  • Giant interfaces with too many optional fields
  • Fragile numeric enums when a union of strings is enough
  • Typing only on the frontend while the backend returns any
  • Lying to the compiler instead of fixing the model

Completion criteria

  • No unjustified any in the areas touched
  • Boundaries validated at runtime
  • Discriminated unions for complex states
  • Project typecheck passing
  • Types readable and aligned with the domain