typescript
via PatrickJS/awesome-cursorrules
TypeScript coding standards and best practices for modern web development
What is typescript?
Establishes TypeScript conventions for type safety, naming, code organization, and error handling. Use this rule to enforce strict typing, consistent naming patterns, and best practices across a TypeScript codebase.
- Enforce strict TypeScript configuration and type safety patterns (avoid any, prefer unknown, use generics)
- Define naming conventions: PascalCase for types, camelCase for variables, UPPER_CASE for constants
- Organize code with co-located types, barrel exports, and dedicated type directories
- Require explicit return types for public functions and implement proper error handling with custom error types
- Apply design patterns (Builder, Repository, Factory) and dependency injection for maintainable architecture
- Use discriminated unions, type guards, and readonly properties for runtime type safety
Applies to
File patterns this rule matches.
Rule definition (reference)
Source of truth, from the repository.
TypeScript Best Practices
Type System
- Prefer interfaces over types for object definitions
- Use type for unions, intersections, and mapped types
- Avoid using
any, preferunknownfor 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
typesdirectory - 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
Related rules
TypeScript development patterns with Axios HTTP client integration.
TypeScript and Google Apps Script development with clasp integration
TypeScript code conventions for Node.js, Next.js, React, Expo, tRPC, and Tailwind projects.
TypeScript, React Native, Expo, Jest, and Detox best practices for mobile development.
TypeScript LLM development with functional programming, multi-provider architecture, and composable APIs.
TypeScript and NestJS development with clean code principles, SOLID design, and modular architecture.
