typescript

Typescript

23 community-ranked rules

23 rules
Helpful7/10
2↑

Prefer small, focused functions under 30 lines. Extract helpers when logic grows.

Saving the agent time by explicitly stating a specific function length preference (under 30 lines) that requires inferring from code patterns otherwise.

Style Guide
TypeScript Defaults
Move to Config2/10
2↑

Use TypeScript strict mode with `strict: true` in `tsconfig.json` — enables `strictNullChecks`, `noImplicitAny`, and other safety checks.

This belongs in tsconfig.json (`strict: true`), not in agent instructions.

Style Guide
TypeScript Defaults
Helpful7/10
2↑

Use `readonly` for arrays and properties that should not be mutated.

Saving the agent time on consistently applying TypeScript immutability best practices without needing to infer from scattered code examples.

Style Guide
TypeScript Defaults
Helpful7/10
2↑

Use discriminated unions for state management over boolean flags.

Saving the agent time by explicitly enforcing a TypeScript best practice for state management that requires code pattern recognition across files.

Style Guide
TypeScript Defaults
Move to Config3/10
1↑

Use Google-style JSDoc docstrings for every public module, class, function, and method.

Documentation formatting should be enforced by static analysis tools like eslint-plugin-jsdoc rather than relying on AI instructions.

Helpful8/10
1↑

If you're unsure whether to use `interface` or `type`, prefer `interface` for object shapes (extendable) and `type` for unions, intersections, and computed types.

Saves the agent time from scanning multiple code files to infer the conditional preference for interface vs type in TypeScript.

Style Guide
TypeScript Defaults
Helpful5/10
1↑

Use plan mode for verification steps, not just building

Adds minimal value over the 0922494eee667d5d rule as it restates plan mode use without distinct actionable guidance.

Agent Workflow
Plan Mode Default
Vague ruleNear duplicate
Helpful8/10
1↑

Use `satisfies` operator for type validation without widening: `const config = { ... } satisfies Config`.

Saves the agent time by directly specifying the `satisfies` operator pattern for TypeScript type validation without widening, which it could learn from docs but benefits from explicit guidance.

Style Guide
TypeScript Defaults
Helpful6/10
1↑

Validate all external inputs at system boundaries. Trust nothing from users, APIs, or files.

Saves time by specifying all three untrusted input sources (users, APIs, files) as requiring validation at system boundaries.

Architecture
Production Readiness
Helpful8/10
1↑

Use template literal types for string pattern validation: `type Route = \`/${string}\``.

Saving the agent time by specifying the exact preferred TypeScript template literal type pattern for string validation like routes, which it could infer from codebase examples but not standard configs.

Style Guide
TypeScript Defaults
1↑

For TypeScript: Only write Vitest tests when resolving a specific user issue or upon explicit request.

Testing
Vitest
1↑

Design a minimal, intuitive public API. Every exported symbol is a commitment — keep the surface area small.

Architecture
Library Architecture
1↑

Avoid N+1 query patterns — use batch loading, JOINs, or DataLoader patterns.

Performance
Performance Guidelines
Move to Config4/10
1↑

Prefer `unknown` over `any` — it forces type narrowing before use and catches bugs at compile time.

Enforce preferring `unknown` over `any` with a linter rule like ESLint `@typescript-eslint/no-explicit-any` or Biome `noExplicitAny`, not in agent instructions.

Style Guide
TypeScript Defaults
Helpful8/10
1↑

Use `WeakMap`/`WeakSet` for caching references that should not prevent garbage collection.

Saves the agent time by directly specifying the precise WeakMap/WeakSet pattern for memory-safe caching instead of reasoning through alternatives like Map/Set.

Style Guide
TypeScript Defaults
Helpful8/10
1↑

If a type is used across more than 2 files, move it to a shared `types/` directory. If it's only used locally, co-locate it.

Saving the agent from analyzing type usage across files to infer the exact threshold of more than 2 files for shared types directory placement.

Style Guide
TypeScript Defaults
Move to Config3/10
1↑

Write rules for yourself that prevent the same mistake

This belongs in automated testing or linting pipelines, not in agent instructions.

Agent Workflow
Self-Improvement Loop
Vague rule
Helpful8/10
1↑

Use `interface` for object shapes that may be extended and `type` for unions, intersections, and mapped types.

Saving the agent from having to discover the preferred TypeScript interface vs type convention from the handbook or existing code patterns.

Style Guide
TypeScript Defaults
Move to Config2/10
1↑

Avoid `any` — use `unknown` with type guards when the type is truly unknown.

Avoiding `any` belongs in an ESLint (@typescript-eslint/no-explicit-any) or Biome (typescript/noExplicitAny) rule instead.

Style Guide
TypeScript Defaults
Essential9/10
1↑ 1↓

If a function can fail, return a discriminated union (`{ ok: true; data: T } | { ok: false; error: E }`) instead of throwing — callers are forced to handle both cases.

The agent cannot infer the non-standard convention of using specific discriminated unions instead of throwing exceptions, which contradicts common TypeScript/JavaScript defaults.

Style Guide
TypeScript Defaults
1↓

Use `React.FC` sparingly — prefer explicit return types: `function Card({ title }: Props): React.ReactElement`. Use generic components: `function List<T>({ items, renderItem }: ListProps<T>)`.

framework
React Defaults
1↓

Write E2E tests for critical user journeys: signup, login, checkout, core workflows.

Testing
E2E Testing
Move to Config3/10
1↓

Use `const` by default; `let` only when reassignment is needed. Never use `var`.

This belongs in ESLint or Biome rules like 'no-var' and 'prefer-const', not in agent instructions.

Style Guide
TypeScript Defaults

Have a rule that helps your AI coding agent? Share it with the community.

Suggest a Rule