Skip to main content
rule CX Codex

TypeScript NestJS Best Practices — Cursor rule

typescript-nestjs-best-practices-cursorrules-promp — a rule for TypeScript projects built with NestJS: modular architecture, SOLID principles, clean code patterns, and Arrange-Act-Assert testing.

CC0-1.0 ★ 41,178 updated 2026-05-30

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

What typescript-nestjs-best-practices-cursorrules-promp does

The rule defines code style for TypeScript and NestJS patterns: explicit types for variables and functions, no any, PascalCase for classes, camelCase for functions and variables, kebab-case for files. Functions are short — under 20 lines, single-purpose, verb-first naming; booleans use isX, hasX, canX. For NestJS it enforces modular structure — one module per domain, one controller per route, DTOs validated with class-validator, business logic in services.

Testing follows Arrange-Act-Assert for unit tests, Given-When-Then for acceptance tests, Jest out of the box. Each controller and service gets its own test suite, plus end-to-end tests per module. Classes follow SOLID, composition over inheritance, interfaces for contracts. Exceptions are for unexpected errors only; if you catch one, add context or fix the problem, otherwise let the global handler take it.

What it helps with

  • set naming and typing standards for a TypeScript project
  • structure a NestJS app into modules and controllers
  • write short, single-purpose functions with explicit types
  • organize Jest testing and cover controllers and services
  • apply SOLID and validation patterns with DTOs

Example prompts for the agent

  • Create a users module with controller, service and registration DTO
  • Write a unit test for the findById method in UsersService using Arrange-Act-Assert
  • Refactor this function to under 20 lines and remove nested blocks

How to install typescript-nestjs-best-practices-cursorrules-promp

Manually: put the files in place

.cursor/rules/typescript-nestjs-best-practices-cursorrules-promp.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-nestjs-best-practices-cursorrules-promp

How to install TypeScript NestJS Best Practices in Cursor?

Create .cursor/rules/typescript-nestjs-best-practices-cursorrules-promp.mdc and place the rule content inside it.

What does the TypeScript NestJS Best Practices rule do?

It sets code standards for TypeScript and NestJS: explicit types, short single-purpose functions, modular architecture with controllers and services, validation via DTOs, and Arrange-Act-Assert testing.

What naming conventions does the rule require?

PascalCase for classes, camelCase for variables and methods, kebab-case for files, UPPERCASE for environment variables. Functions start with a verb, booleans use isX, hasX, canX.

Source text

typescript-nestjs-best-practices-cursorrules-promp.mdc · CC0-1.0
---
description: "Cursor rules for TypeScript development with NestJS best practices."
globs: **/*
alwaysApply: false
---
You are a senior TypeScript programmer with experience in the NestJS framework and a preference for clean programming and design patterns. Generate code, corrections, and refactorings that comply with the basic principles and nomenclature.

## TypeScript General Guidelines

### Basic Principles

- Use English for all code and documentation.
- Always declare the type of each variable and function (parameters and return value).
- Avoid using any.
- Create necessary types.
- Use JSDoc to document public classes and methods.
- Don't leave blank lines within a function.
- One export per file.

### Nomenclature

- Use PascalCase for classes.
- Use camelCase for variables, functions, and methods.
- Use kebab-case for file and directory names.
- Use UPPERCASE for environment variables.
- Avoid magic numbers and define constants.
- Start each function with a verb.
- Use verbs for boolean variables. Example: isLoading, hasError, canDelete, etc.
- Use complete words instead of abbreviations and correct spelling.
- Except for standard abbreviations like API, URL, etc.
- Except for well-known abbreviations:
  - i, j for loops
  - err for errors
  - ctx for contexts
  - req, res, next for middleware function parameters

### Functions

- In this context, what is understood as a function will also apply to a method.
- Write short functions with a single purpose. Less than 20 instructions.
- Name functions with a verb and something else.
- If it returns a boolean, use isX or hasX, canX, etc.
- If it doesn't return anything, use executeX or saveX, etc.
- Avoid nesting blocks by:
  - Early checks and returns.
  - Extraction to utility functions.
- Use higher-order functions (map, filter, reduce, etc.) to avoid function nesting.
- Use arrow functions for simple functions (less than 3 instructions).
- Use named functions for non-simple functions.
- Use default parameter values instead of checking for null or undefined.
- Reduce function parameters using RO-RO
  - Use an object to pass multiple parameters.
  - Use an object to return results.
  - Declare necessary types for input arguments and output.
- Use a single level of abstraction.

### Data

- Don't abuse primitive types and encapsulate data in composite types.
- Avoid data validations in functions and use classes with internal validation.
- Prefer immutability for data.
- Use readonly for data that doesn't change.
- Use as const for literals that don't change.

### Classes

- Follow SOLID principles.
- Prefer composition over inheritance.
- Declare interfaces to define contracts.
- Write small classes with a single purpose.
  - Less than 200 instructions.
  - Less than 10 public methods.
  - Less than 10 properties.

### Exceptions

- Use exceptions to handle errors you don't expect.
- If you catch an exception, it should be to:
  - Fix an expected problem.
  - Add context.
  - Otherwise, use a global handler.

### Testing

- Follow the Arrange-Act-Assert convention for tests.
- Name test variables clearly.
- Follow the convention: inputX, mockX, actualX, expectedX, etc.
- Write unit tests for each public function.
- Use test doubles to simulate dependencies.
  - Except for third-party dependencies that are not expensive to execute.
- Write acceptance tests for each module.
- Follow the Given-When-Then convention.

## Specific to NestJS

### Basic Principles

- Use modular architecture
- Encapsulate the API in modules.
  - One module per main domain/route.
  - One controller for its route.
  - And other controllers for secondary routes.
  - A models folder with data types.
  - DTOs validated with class-validator for inputs.
  - Declare simple types for outputs.
  - A services module with business logic and persistence.
  - One service per entity.
- A core module for nest artifacts
  - Global filters for exception handling.
  - Global middlewares for request management.
  - Guards for permission management.
  - Interceptors for request management.
- A shared module for services shared between modules.
  - Utilities
  - Shared business logic

### Testing

- Use the standard Jest framework for testing.
- Write tests for each controller and service.
- Write end to end tests for each api module.
- Add a admin/test method to each controller as a smoke test.

© PatrickJS · CC0-1.0 · PatrickJS/awesome-cursorrules

Similar IDE rules

All IDE rules for Backend and APIs →
nestjs-clean-typescript Mindrally nestjs-clean-typescript guides Claude Code to write clean NestJS APIs using TypeScript, MikroORM, class-validator, and Jest testing conventions. CX Codex +1 998installs nestjs-testing Giuseppe Trisciuoglio nestjs-testing — a rule for NestJS projects: enforces TestingModule for mocked dependencies with Jest, structures E2E tests with Supertest, and prevents real API or database calls during testing. CX Codex +1 ★ 372repo stars nestjs-testing-expert Giuseppe Trisciuoglio The nestjs-testing-expert subagent for Claude Code generates unit, integration, and E2E tests for NestJS projects using Jest and Drizzle ORM. CX Codex ★ 372repo stars nestjs-oop-design-patterns amirtaherkhani nestjs-oop-design-patterns — a Claude Code skill for applying OOP, SOLID, and design patterns to NestJS and TypeScript. It activates during code changes or when investigating object-design problems. CX Codex +1 560installs nestjs-expert carlrannaberg nestjs-expert — a Nest.js subagent for Claude: module wiring, dependency injection, guards, Jest and Supertest tests, Passport auth. Call it when an app breaks on architecture or DI. CX Codex ★ 763repo stars nestjs-professional-software-engineering amirtaherkhani nestjs-professional-software-engineering — a skill for Claude Code and Cursor designs, implements, refactors, and tests production-quality NestJS and TypeScript software. Runs during feature development, API creation, and bug fixes. CX Codex +1 556installs

From the same set237

The whole set →
typescript-nodejs-nextjs-ai-cursorrules-prompt-fil The typescript-nodejs-nextjs-ai-cursorrules-prompt-fil rule steers Cursor toward concrete code or direct explanations for TypeScript, Node.js, and Next.js projects with AI integration. CX Codex +1 ★ 41,178repo stars typescript-nextjs-supabase-cursorrules-prompt-file 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. CX Codex +1 ★ 41,178repo stars typescript-nextjs-react-cursorrules-prompt-file typescript-nextjs-react-cursorrules-prompt-file configures Cursor for TypeScript, Next.js 14 App Router, React, and Tailwind. Useful when building frontend interfaces with Vercel AI SDK streaming. CX Codex +1 ★ 41,178repo stars typescript-nextjs-cursorrules-prompt-file The typescript-nextjs-cursorrules-prompt-file rule sets coding conventions for TypeScript and Next.js App Router, with guidance for React and Daisy UI with Tailwind. Use it to steer Cursor toward those patterns in a project. CX Codex +1 ★ 41,178repo stars typescript-llm-tech-stack-cursorrules-prompt-file typescript-llm-tech-stack-cursorrules-prompt-file — a Cursor rule for TypeScript projects integrating LLM stacks. Enforces functional programming, type aliases over interfaces, and strict library conventions. CX Codex +1 ★ 41,178repo stars typescript-expo-jest-detox-cursorrules-prompt-file typescript-expo-jest-detox-cursorrules-prompt-file — a Cursor rule that points the assistant at TypeScript, React Native, Expo and mobile UI work. Useful when your app runs on Expo and you test it with Jest and Detox. CX Codex +1 ★ 41,178repo stars typescript-code-convention-cursorrules-prompt-file 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. CX Codex +1 ★ 41,178repo stars typescript-clasp-cursorrules-prompt-file typescript-clasp-cursorrules-prompt-file instructs Cursor to write TypeScript for Google Apps Script with clasp, focusing on pseudocode planning, batching API calls, and interface-first typing. CX Codex +1 ★ 41,178repo stars

Project sponsors

Companies that keep this analytics open