Skip to content

Latest commit

 

History

History
64 lines (48 loc) · 4.12 KB

rules-typescript-react.md

File metadata and controls

64 lines (48 loc) · 4.12 KB
paths
**/*.ts
**/*.tsx
**/package.json

TypeScript React application rules

These rules apply to components, hooks, routes, API clients, tests, and review comments for this React application.

Stack and architecture

  • Use TypeScript in strict mode, React function components, typed props, and named exports unless the existing file or framework requires otherwise.
  • Keep components presentational where practical. Put remote data access in API or query modules and reusable behavior in hooks.
  • Keep server state, URL state, local UI state, and form state separate. Do not copy server data into useState unless mutation or local editing truly requires it.
  • Co-locate component-specific types, tests, and styles with the component when that matches the project layout.
  • Never use any to silence a type error. Use unknown, generics, narrowing, or an explicit type boundary.

Data boundaries

  • Treat API responses as untyped external data. Validate or narrow them at the client boundary before rendering.
  • Model loading, empty, error, permission-denied, partial-data, and retry states. Do not design only for populated success data.
  • Make mutations explicit about optimistic behavior, rollback, cache invalidation, and duplicate submission prevention.
  • Use abort signals or equivalent cancellation for requests whose result can become irrelevant.
  • Never expose service tokens, signing secrets, or privileged API keys in browser code.

Accessibility and interaction

  • Use semantic HTML before ARIA. Buttons are buttons, links navigate, inputs have labels, and tables are real tables.
  • Every interactive element must work with a keyboard. Preserve visible focus, logical tab order, and Escape behavior where applicable.
  • Dialogs must manage initial focus, trap focus while open when appropriate, restore focus on close, and expose correct accessible names.
  • Forms need labels, helpful error text, programmatic error association, autocomplete where appropriate, and protection against duplicate submits.
  • Respect reduced-motion preferences and do not communicate meaning by color alone.

React correctness and performance

  • Keep rendering deterministic. Do not mutate props or shared state during render.
  • Use effects only for synchronization with external systems. Derived state should usually be computed during render.
  • Clean up subscriptions, timers, event listeners, and in-flight side effects.
  • Use keys that identify data rather than array indexes when order or identity can change.
  • Measure before optimizing renders. Prefer fixing slow data access, layout work, or repeated serialization before broad memoization.
  • Avoid network waterfalls. Parallelize independent fetches and sequence only true dependencies.

Styling and design-system use

  • Use the established design system and spacing scale before inventing one-off styles.
  • Do not copy large style blocks between components. Extract a reusable pattern only after the duplication is real.
  • Keep visual states understandable at 200 percent zoom and with narrow viewport widths.
  • Do not use fixed pixel heights that clip dynamic or localized content.

Testing and verification

  • Add or update tests for behavior users depend on. Prefer accessible queries and user-event interactions over implementation-specific selectors.
  • Cover loading, empty, error, validation, authorization denial, and successful completion for important workflows.
  • Test keyboard interaction and focus behavior for custom interactive components.
  • Use deterministic fake timers, fixed clocks, and controlled network mocks. Do not use arbitrary sleeps.
  • Before finishing, run the narrowest relevant tests first, then broader checks when practical. Report exact commands and results.

Change discipline

  • Read neighboring components and hooks before editing. Follow established project patterns unless the user explicitly asks for a migration.
  • Make the smallest complete change. Do not mix visual redesign, refactoring, and behavior changes in one edit.
  • If design intent is ambiguous and a wrong guess affects accessibility, data loss, payment, privacy, or destructive behavior, ask before implementing.