Skip to main content
rule

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.

Install into Cursor · Manually: put the files in place All methods and platforms ↓
.cursor/rules/typescript.mdc
Open on GitHub

What 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 any out 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 types directory
  • Rewrite this function with async/await and add an explicit return type
  • Replace any with unknown and add a type guard before using it

How to install typescript

Manually: put the files in place

.cursor/rules/typescript.mdc

Frontmatter: description, globs, alwaysApply

Step 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

Similar IDE rules

All IDE rules for Programming languages →

From the same set68

vue A Cursor rule that tells the agent how to write Vue: Composition API, Pinia, typing and routing. Useful when the project already has .vue and .ts files. ★ 41,011repo stars vue-3-nuxt-3-typescript-cursorrules-prompt-file A Cursor rule that tells the agent how to write Vue 3 and Nuxt 3 code: Composition API, TypeScript interfaces, Nuxt conventions. Useful when the project already runs this stack. ★ 41,011repo stars vitest-unit-testing-cursorrules-prompt-file A Cursor rule that turns the agent into a Vitest and TypeScript test writer: it targets business logic and utilities, mocking dependencies with vi.mock. ★ 41,011repo stars unity-cursor-ai-c-cursorrules-prompt-file A Cursor rule that briefs the assistant on a Unity game project in C#: modular code, responsive mechanics, real-time performance. ★ 41,011repo stars typescript-vite-tailwind-cursorrules-prompt-file A Cursor rule that pins down TypeScript code style for Vue projects built with Vite and styled with Tailwind. Useful when you want the agent's output to stay predictable. ★ 41,011repo stars typescript-nodejs-nextjs-react-ui-css-cursorrules- A Cursor rule that pins down how the agent writes TypeScript, Next.js App Router, React and Tailwind code. Useful when generated components drift from your conventions. ★ 41,011repo stars typescript-nextjs-supabase-cursorrules-prompt-file A Cursor rule that keeps TypeScript work on Next.js App Router and Supabase consistent: server components first, Shadcn UI, Tailwind. ★ 41,011repo stars typescript-code-convention-cursorrules-prompt-file A Cursor rule that pins down TypeScript code style for Next.js App Router, React, Expo, tRPC and Tailwind projects. Useful when the agent writes code inconsistently. ★ 41,011repo stars

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor