TypeScript
Types
- •Define precise types; avoid
any— useunknownand narrow instead - •Prefer type inference where types are obvious
- •Use union types and discriminated unions for known value sets
- •Use
readonlyfor data that shouldn't be mutated - •Avoid type assertions (
as); prefer type guards - •Use
as constfor literal values that shouldn't widen - •Prefer
interfacefor object shapes that may be extended; usetypefor unions, intersections, and mapped types
Patterns
- •Use
satisfiesto validate a value matches a type while preserving its narrower inferred type - •Narrow with control flow (
if,in,instanceof) instead of manual type predicates when possible - •Use utility types (
Pick,Omit,Partial,Required) to derive types instead of redeclaring fields - •Type function boundaries explicitly (parameters and return types); let internals be inferred
- •Use generics to preserve caller-side type information; avoid forcing callers to assert