Coding Standards Quick Reference
TypeScript Patterns
Discriminated Unions (preferred for state)
typescript
type AsyncState<T> =
| { status: "idle" }
| { status: "loading" }
| { status: "success"; data: T }
| { status: "error"; error: Error };
Result Pattern (preferred for error handling)
typescript
type Result<T, E = Error> =
| { ok: true; value: T }
| { ok: false; error: E };
Branded Types (for type-safe IDs)
typescript
type UserId = string & { readonly __brand: unique symbol };
const createUserId = (id: string): UserId => id as UserId;
Type Narrowing with satisfies
typescript
const config = {
apiUrl: "https://api.example.com",
timeout: 5000,
} satisfies Config; // Validates type but preserves literal types
as const over Enums
typescript
const Status = {
Active: "active",
Inactive: "inactive",
Pending: "pending",
} as const;
type Status = (typeof Status)[keyof typeof Status];
Import Order
- •Built-in (
node:*) - •External (
react,next) - •Internal (
@/lib/*,@/components/*) - •Relative (
./,../)
Key Rules
- •No
any(useunknown+ type guards) - •No default exports (except Next.js pages/layouts)
- •No enums (use
as const) - •Explicit return types on exported functions
- •
constby default,letonly when needed - •Early returns over nested conditionals
- •Zod for runtime validation at boundaries
References
- •See
rules/common/code-style.mdfor full style guide - •See
rules/nextjs/components.mdfor component patterns