Skip to main content
RapidDev - Software Development Agency
cursor-tutorial

How to Enforce Strict Typing with Cursor

Cursor often generates loosely typed NestJS code with 'any' types, partial interfaces, and missing decorators. By adding project rules that enforce strict TypeScript mode, referencing your existing entity files with @file, and prompting with explicit typing requirements, you can ensure Cursor generates properly decorated NestJS classes with full type safety including DTOs, entities, and validation pipes.

What you'll learn

  • How to create rules that enforce strict typing in NestJS code
  • How to get Cursor to generate properly decorated entities and DTOs
  • How to use @file to reference existing types as patterns for new code
  • How to enforce class-validator decorators in Cursor-generated DTOs
Book a free consultation
4.9Clutch rating
600+Happy partners
17+Countries served
190+Team members
Beginner7 min read10-15 minCursor Free+, NestJS/TypeScript projectsMarch 2026RapidDev Engineering Team
TL;DR

Cursor often generates loosely typed NestJS code with 'any' types, partial interfaces, and missing decorators. By adding project rules that enforce strict TypeScript mode, referencing your existing entity files with @file, and prompting with explicit typing requirements, you can ensure Cursor generates properly decorated NestJS classes with full type safety including DTOs, entities, and validation pipes.

Enforcing strict typing in Cursor-generated NestJS code

NestJS relies heavily on TypeScript decorators, class-validator, and class-transformer for type-safe APIs. Cursor can generate syntactically correct NestJS code that lacks proper decorators, uses any types, or skips validation. This tutorial configures Cursor to produce fully typed, decorated NestJS code that matches your project's standards.

Prerequisites

  • Cursor installed with a NestJS project open
  • TypeScript strict mode enabled in tsconfig.json
  • class-validator and class-transformer installed
  • Familiarity with NestJS decorators and modules

Step-by-step guide

1

Create a NestJS strict typing rule

Add a project rule that enforces TypeScript strict mode conventions specifically for NestJS patterns. Include requirements for decorators, DTOs, entities, and service methods. NestJS-specific patterns need explicit rules because Cursor may generate plain TypeScript instead of decorated classes.

.cursor/rules/nestjs-typing.mdc
1---
2description: Enforce strict typing in NestJS code
3globs: "*.ts"
4alwaysApply: true
5---
6
7# NestJS Strict Typing Rules
8
9## TypeScript:
10- NEVER use 'any' type use 'unknown' and narrow, or define proper types
11- NEVER use type assertions (as) unless narrowing from unknown
12- ALWAYS use explicit return types on all public methods
13- ALWAYS enable strict mode patterns: strictNullChecks, noImplicitAny
14
15## DTOs:
16- ALWAYS use class-validator decorators on every DTO property
17- ALWAYS use class-transformer @Type() for nested objects
18- ALWAYS use @ApiProperty() from @nestjs/swagger on DTO properties
19- ALWAYS create separate CreateDto, UpdateDto (with PartialType), ResponseDto
20- Use readonly properties in response DTOs
21
22## Entities:
23- ALWAYS use TypeORM decorators: @Entity, @Column, @PrimaryGeneratedColumn
24- ALWAYS specify column types explicitly: @Column({ type: 'varchar', length: 255 })
25- ALWAYS define relations with explicit types and decorators
26
27## Services:
28- ALWAYS define return types as Promise<EntityType> or Promise<EntityType[]>
29- ALWAYS handle null returns explicitly with proper error throwing
30- Use custom exception classes extending HttpException

Expected result: Cursor generates NestJS code with full TypeScript strict typing, class-validator decorators, and explicit return types.

2

Reference existing entities as patterns for new code

When asking Cursor to generate a new module, reference an existing well-typed entity with @file. Cursor pattern-matches against the referenced file, producing new code that follows the same decoration and typing conventions.

Cmd+L prompt
1@nestjs-typing.mdc @src/users/entities/user.entity.ts
2
3Create a complete Product module following the exact same patterns
4as the User entity. Include:
51. product.entity.ts TypeORM entity with proper column types
62. create-product.dto.ts with class-validator decorators
73. update-product.dto.ts using PartialType(CreateProductDto)
84. product-response.dto.ts readonly response with @ApiProperty
95. products.service.ts with explicit Promise return types
106. products.controller.ts with full Swagger decorators
11
12The Product has: id (uuid), name (string 255), price (decimal),
13category (string 100), isActive (boolean), createdAt, updatedAt.

Pro tip: Always reference an existing file that follows your conventions. Cursor's pattern matching from real project files is more reliable than rules alone for complex decorator patterns.

Expected result: Cursor generates six files matching the User entity patterns with full TypeORM and class-validator decorators.

3

Generate a type-safe DTO with validation using Cmd+K

Open a new file, type a basic class skeleton, select it, and press Cmd+K. Ask Cursor to add all necessary decorators and validation. This is faster than generating from scratch when you know the structure you want.

Cmd+K prompt
1Add strict typing, class-validator decorators, and @ApiProperty to this
2NestJS DTO. Every string needs @IsString() and @MaxLength().
3Every number needs @IsNumber() and @Min/@Max.
4Optional fields need @IsOptional(). Nested objects need @ValidateNested()
5and @Type(). Add @ApiProperty with description and example for each field.

Expected result: Cursor adds class-validator decorators, @ApiProperty with descriptions, and proper type constraints to every property.

4

Audit existing code for type safety gaps

Use Cursor Chat with @codebase to scan your NestJS project for type safety issues. This catches any types, missing decorators, and implicit return types that could cause runtime errors or Swagger documentation gaps.

Cmd+L prompt
1@nestjs-typing.mdc @codebase
2
3Audit this NestJS project for type safety issues. Find and list:
41. Any use of 'any' type in services, controllers, or DTOs
52. DTO properties missing class-validator decorators
63. Service methods without explicit Promise return types
74. Entity columns without explicit type definitions
85. Controllers missing @ApiResponse decorators
9
10For each issue, show the file, line, and the corrected version.

Expected result: Cursor lists all type safety gaps in your NestJS project with specific file locations and corrected code for each.

5

Set up a custom command for DTO generation

Create a reusable Cursor custom command that generates fully typed DTOs on demand. Save this in .cursor/commands/ so any team member can type /dto in Chat to generate a new DTO set following your standards.

.cursor/commands/dto.md
1---
2description: Generate a type-safe NestJS DTO set
3---
4
5Generate a complete set of NestJS DTOs for the entity described by the user.
6
7Follow these rules:
81. CreateDto all required fields with class-validator decorators
92. UpdateDto extends PartialType(CreateDto)
103. ResponseDto readonly fields with @Exclude() for sensitive data
114. QueryDto optional filter parameters with @IsOptional()
12
13Every property must have:
14- @ApiProperty() with description and example
15- Appropriate class-validator decorator
16- Explicit TypeScript type (never any)
17
18Reference @nestjs-typing.mdc for full conventions.

Expected result: Team members can type /dto in Cursor Chat followed by an entity description to generate a complete DTO set.

Complete working example

src/products/dto/create-product.dto.ts
1import { ApiProperty } from '@nestjs/swagger';
2import {
3 IsString,
4 IsNumber,
5 IsBoolean,
6 IsOptional,
7 MaxLength,
8 Min,
9 Max,
10 IsNotEmpty,
11} from 'class-validator';
12
13export class CreateProductDto {
14 @ApiProperty({ description: 'Product name', example: 'Wireless Keyboard' })
15 @IsString()
16 @IsNotEmpty()
17 @MaxLength(255)
18 readonly name: string;
19
20 @ApiProperty({ description: 'Price in USD', example: 49.99 })
21 @IsNumber({ maxDecimalPlaces: 2 })
22 @Min(0)
23 @Max(999999.99)
24 readonly price: number;
25
26 @ApiProperty({ description: 'Product category', example: 'Electronics' })
27 @IsString()
28 @IsNotEmpty()
29 @MaxLength(100)
30 readonly category: string;
31
32 @ApiProperty({ description: 'Whether product is active', example: true, required: false })
33 @IsOptional()
34 @IsBoolean()
35 readonly isActive?: boolean;
36
37 @ApiProperty({ description: 'Product description', example: 'A compact wireless keyboard', required: false })
38 @IsOptional()
39 @IsString()
40 @MaxLength(2000)
41 readonly description?: string;
42}

Common mistakes when enforcing Strict Typing with Cursor

Why it's a problem: Not specifying NestJS-specific decorators in rules

How to avoid: Create NestJS-specific rules that list every required decorator pattern for entities, DTOs, and controllers.

Why it's a problem: Using interface DTOs instead of class DTOs

How to avoid: Add to your rules: ALWAYS use classes for DTOs, never interfaces. Interfaces do not work with NestJS validation pipes.

Why it's a problem: Accepting Cursor output with 'any' return types on service methods

How to avoid: Add a rule mandating explicit Promise<EntityType> return types. Use the audit prompt periodically to catch any types that slip through.

Best practices

  • Reference an existing well-typed entity file with @file when generating new modules
  • Create separate Create, Update, Response, and Query DTOs for each entity
  • Use PartialType and PickType from @nestjs/swagger for DTO composition
  • Add @ApiProperty to every DTO field for automatic Swagger documentation
  • Create custom commands in .cursor/commands/ for repeatable DTO generation
  • Audit for 'any' types periodically using @codebase searches
  • Enable TypeScript strict mode in tsconfig.json to complement Cursor rules

Still stuck?

Copy one of these prompts to get a personalized, step-by-step explanation.

ChatGPT Prompt

Generate a type-safe NestJS module for a Product entity with TypeORM decorators, class-validator DTOs, a service with explicit return types, and a controller with Swagger decorators. Use strict TypeScript with no 'any' types.

Cursor Prompt

@nestjs-typing.mdc @src/users/entities/user.entity.ts Create a complete Order module matching the User entity patterns. Include entity with TypeORM decorators, Create/Update/Response DTOs with class-validator, service with explicit Promise return types, and controller with @ApiResponse decorators.

Frequently asked questions

Why does Cursor use 'any' even when strict mode is enabled?

TypeScript strict mode is a compiler setting, but Cursor does not read tsconfig.json during generation. You must explicitly tell Cursor in your rules to never use any and always use explicit types.

Should I use interfaces or classes for NestJS DTOs?

Always use classes. NestJS validation pipes and class-transformer require classes because TypeScript interfaces are erased at runtime and cannot be validated.

How do I handle nested object validation?

Use @ValidateNested() from class-validator and @Type(() => NestedDto) from class-transformer. Add this pattern to your rules file with an example.

Can Cursor generate TypeORM migrations from entities?

Cursor can generate migration files if you provide the entity as context, but TypeORM's migration:generate CLI command is more reliable since it diffs against the actual database schema.

What about Prisma instead of TypeORM?

If using Prisma, update your rules to reference Prisma schema files and generated types. Cursor can generate NestJS services that use Prisma Client with full type safety from the generated types.

Can RapidDev help set up type-safe NestJS architecture?

Yes. RapidDev helps teams design NestJS module structures with strict typing, validation, and Swagger documentation, and configures Cursor rules to maintain these standards across the team.

RapidDev

Talk to an Expert

Our team has built 600+ apps. Get personalized help with your project.

Book a free consultation

Need help with your project?

Our experts have built 600+ apps and can accelerate your development. Book a free consultation — no strings attached.

Book a free consultation

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We'll discuss your project and provide a custom quote at no cost.