TypeScript Standards
Imports
- •ALWAYS
import, NEVERrequire() - •Separate value imports and
import type— types last - •Use
@/path alias when relative path has more than one../ - •NEVER import an entire namespace (
import * as X) — destructure
Functions
- •ALWAYS arrow functions:
const fn = () => {}— NEVERfunctiondeclarations - •Exception: exported module-level functions that need hoisting are acceptable
Types
- •NEVER
any— use proper types orunknown+ type guards - •NEVER
ascasting — prefer type guards, generics, or Zod parsing (as constis OK) - •Prefer
interfacefor object shapes,typefor unions/intersections/utilities - •Discriminated unions over optional fields for variant types
Patterns
- •Result pattern for data fetching:
type Result<T, E = Error> = [E, null] | [null, T]— NEVER throw for expected errors - •Early returns over nested conditionals
- •Destructure function params and object access
- •
Array.findinside a loop = build aMapfirst, O(1) lookups - •
sort()mutates — usetoSorted()or spread + sort - •Nullish coalescing (
??) over logical OR (||) for defaults - •Optional chaining (
?.) — but NEVER more than 3 levels deep; refactor the type instead - •
constby default,letonly when reassignment is necessary, NEVERvar - •Prefer
satisfiesoverasfor type-checking without widening - •Use
Map/Setover plain objects when keys are dynamic - •Enums: prefer
as constobjects over TypeScriptenum