Home › All rules › Community › TypeScript Cursor Rules

TypeScript Cursor Rules

TypeScript coding standards and best practices for modern web development

CursorTypeScriptCommunity (CC0)

How to use

Save this as .cursor/rules/typescript.mdc in your project, commit it, and Cursor will load it. Review it first: community files can be opinionated or out of date.

Note: Review and test before use. Provided as is, without warranty. See Terms.

.cursor/rules/typescript.mdcDownload  
---
description: "TypeScript coding standards and best practices for modern web development"
globs: **/*.ts, **/*.tsx, **/*.d.ts
alwaysApply: false
---
# 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

More: TypeScript rules · all community rules

Related rules

TypeScript npm library AGENTS.md

AGENTS.md for publishing TypeScript npm packages: ESM exports maps, bundling with tsdown, type checks, and changesets.

AGENTS.mdTypeScript

TypeScript strict mode rules

Cursor rules for strict TypeScript 5.9+: no any, unknown narrowing, discriminated unions, satisfies, and runtime validation.

CursorTypeScript

TypeScript Clasp Cursor Rules

Cursor rules for TypeScript development with Clasp app script integration.

CursorTypeScriptCommunity (CC0)

TypeScript Code Convention Cursor Rules

Cursor rules for TypeScript development with code convention integration.

CursorTypeScriptCommunity (CC0)

TypeScript Llm Tech Stack Cursor Rules

Cursor rules for TypeScript development with LLM tech stack integration.

CursorTypeScriptCommunity (CC0)

Vscode Extension Dev TypeScript Cursor Rules

Cursor rules for Vscode Extension Dev Typescript.

CursorTypeScriptCommunity (CC0)

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.