typescript — Cursor rule: TypeScript standards for your project
A Cursor rule that pins down how the agent writes TypeScript: types, naming, error handling. Useful when several people touch the same codebase.
.cursor/rules/typescript.mdcWhat typescript does
The rule spells out type conventions: interfaces for object shapes, type for unions and mapped types, unknown instead of any. Strict mode in tsconfig.json is listed as a requirement, not a suggestion.
Naming is fixed too — PascalCase for types, camelCase for variables and functions, UPPER_CASE for constants, a Props prefix on React component props. Type definitions should sit near the code that uses them; shared ones go into a types directory and get re-exported through index.ts.
Functions and errors come next: explicit return types on public functions, async/await over promise chains, custom error classes for domain failures, null checks. There is also a patterns section covering Builder, Repository, Factory and dependency injection. The rule is scoped by the globs **/*.ts, **/*.tsx, **/*.d.ts, so it only applies to TypeScript files and leaves the rest of the codebase alone.
What it helps with
- settle on one type style across a team
- keep
anyout of new code - organise shared types into folders
- model errors with custom classes
- name React component props consistently
Example prompts for the agent
- Write an interface for this API response and put it in the
typesdirectory - Rewrite this function with
async/awaitand add an explicit return type - Replace
anywithunknownand add a type guard before using it
How to install typescript
Manually: put the files in place
.cursor/rules/typescript.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.claude/rules/typescript.mdThe paths frontmatter field limits the files: move globs from the source into paths
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.github/instructions/typescript.instructions.mdThe applyTo field sets which files it applies to: move globs from the source into applyTo
Manually: put the files in place
.devin/rules/typescript.mdManually: put the files in place
.clinerules/typescript.mdStep by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules
Questions about typescript
How do I install the typescript rule in Cursor?
Create the file .cursor/rules/typescript.mdc and paste the rule contents into it.
What does the typescript rule do?
It gives the Cursor agent TypeScript conventions: interfaces over types for objects, camelCase and PascalCase naming, custom error classes, explicit return types.
Which files does the rule apply to?
Files matching **/*.ts, **/*.tsx and **/*.d.ts. The rest of the codebase is untouched.
Source text
typescript.mdc · CC0-1.0
--- 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
© PatrickJS · CC0-1.0 · PatrickJS/awesome-cursorrules