No. 132 Write the change

TypeScript Best Practices

Use when the behavior is already chosen and a file should change

  • Claude Code
  • Codex
  • Cursor
  • Write the change

Prompt

# TypeScript Best Practices

## Type System
- Prefer interfaces over types for object definitions
- Use type for unions, intersections, and mapped types
- Avoid using `any`, prefer `unknown` for unknown types
- Use strict TypeScript configuration
- Leverage TypeScript's built-in utility types
- Use generics for reusable type patterns

## Naming Conventions
- Use PascalCase for type names and interfaces
- Use camelCase for variables and functions
- Use UPPER_CASE for constants
- Use descriptive names with auxiliary verbs (e.g., isLoading, hasError)
- Prefix interfaces for React props with 'Props' (e.g., ButtonProps)

## Code Organization
- Keep type definitions close to where they're used
- Export types and interfaces from dedicated type files when shared
- Use barrel exports (index.ts) for organizing exports
- Place shared types in a `types` directory
- Co-locate component props with their components

## Functions
- Use explicit return types for public functions
- Use arrow functions for callbacks and methods
- Implement proper error handling with custom error types
- Use function overloads for complex type scenarios
- Prefer async/await over Promises

## Best Practices
- Enable strict mode in tsconfig.json
- Use readonly for immutable properties
- Leverage discriminated unions for type safety
- Use type guards for runtime type checking
- Implement proper null checking
- Avoid type assertions unless necessary

## Error Handling
- Create custom error types for domain-specific errors
- Use Result types for operations that can fail
- Implement proper error boundaries
- Use try-catch blocks with typed catch clauses
- Handle Promise rejections properly

## Patterns
- Use the Builder pattern for complex object creation
- Implement the Repository pattern for data access
- Use the Factory pattern for object creation
- Leverage dependency injection
- Use the Module pattern for encapsulation

When to use

“TypeScript Best Practices” fits when the behavior is already chosen and a file should change, because it says “TypeScript Best Practices”, and it is written to stay in force on later turns rather than to finish one task.

With “TypeScript Best Practices” loaded, the user message can be as small as “fix the empty-cart total in src/cart.js and do not touch the test runner”, because “TypeScript Best Practices” is already in force.

When not to use

Leave “TypeScript Best Practices” unused when you only wanted the file explained, or two designs are still open, because “TypeScript Best Practices” points the other way

How to run it

Claude Code

Claude Code loads “TypeScript Best Practices” from .claude/rules on later sessions, so “TypeScript Best Practices” survives a new chat, which a one-off paste does not.

Codex

Codex keeps “TypeScript Best Practices” only if you put “TypeScript Best Practices” in AGENTS.md, or in AGENTS.override.md beside the directory that should inherit it. A paste that is not saved is gone next thread.

Cursor

Cursor keeps “TypeScript Best Practices” by saving “TypeScript Best Practices” as .cursor/rules. The chat transcript alone will not reload it tomorrow.

Comparison

对比待补

This before-and-after was not run on this machine.

Related

来源:PatrickJS awesome-cursorrules · https://github.com/PatrickJS/awesome-cursorrules/blob/main/rules/typescript.mdc · CC0-1.0 · 2026-12-01