TypeScript strict-mode coding standards: naming, where to annotate versus infer, avoiding any and unsafe assertions, enums versus literal unions, and one convention for absent values.
.cursor/rules/typescript-strict-mode-standards.mdc. Downloaded files need to be placed in that repository path.---
description: "TypeScript strict-mode coding standards: naming, where to annotate versus infer, avoiding any and unsafe assertions, enums versus literal unions, and one convention for absent values."
globs: "**/*"
alwaysApply: false
---
Apply these standards to TypeScript code compiled with `strict` enabled.
## Principles
- Type safety: every value has a known type at compile time; values from outside the program (JSON, environment, user input) are validated before use.
- Catch errors at compile time: rely on `strict` and the extra checks the project enables, and keep `tsc --noEmit` passing in CI.
## Naming
- `camelCase` for variables and functions (`userName`, `getUserName`).
- `PascalCase` for classes, interfaces, type aliases and enums (`User`, `UserRole`). Do not prefix interfaces with `I`.
- `UPPER_SNAKE_CASE` only for true constants defined at module level.
## Annotate or infer
- Annotate function parameters, and the return types of exported functions and public methods (they document the contract and stop accidental changes).
- Let TypeScript infer local variables initialized with a value: `let userName = 'John Doe'` is inferred as `string`, not `any`.
- Variables declared without an initializer need an annotation (`let total: number;`). With `noImplicitAny`, parameters without a type are an error.
## any, unknown and assertions
- Do not use `any`. Use `unknown` for values of unknown shape and narrow them, or validate with a schema.
- Avoid `as` assertions and the non-null `!` operator; narrow instead. If one is unavoidable, add a comment explaining why it is safe. `as const` is fine.
## Interfaces, types and enums
- Use interfaces or type aliases for object shapes consistently; use type aliases for unions.
- Prefer string-literal unions or `as const` objects over `enum`. Enums generate runtime code and are rejected by `--erasableSyntaxOnly`, which matches what Node's built-in type stripping supports. Keep existing enums when changing them would be churn.
```ts
const Status = { Active: 'active', Disabled: 'disabled' } as const;
type Status = (typeof Status)[keyof typeof Status]; // 'active' | 'disabled'
```
## Absent values
- Choose one convention per boundary: optional properties (`name?: string`) for "may be missing", `null` for "explicitly empty" when an API or database uses it. Do not mix `null` and `undefined` for the same meaning.
## Type guards
- Narrow with `typeof`, `instanceof`, `in`, discriminant properties or user-defined guards before using a value of a union type.
## Code organization
- Use ES module `import`/`export` at the top of files; use `import type` for type-only imports.
- Group files by feature and keep names consistent with the existing project.
## Pitfalls
- `any` (explicit or through untyped libraries): add types or declare a typed wrapper.
- Unused variables and parameters: enable `noUnusedLocals`/`noUnusedParameters` or the equivalent lint rule.
- Unreachable code after `return`/`throw`.
- `as any` or double assertions (`as unknown as T`) to silence errors.
## Examples
```ts
// Inferred local: type is string
let userName = 'John Doe';
// Annotated exported function
export function getUserName(user: { name: string }): string {
return user.name;
}
// Interface with specific types
interface User {
name: string;
age: number;
}
// Instead of: interface User { name: any; age: number }
```
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Cursor
coding
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A team style guide (this page's earlier version) told developers that `let userName = 'John Doe'` is an implicit any and required I-prefixed interfaces and enums everywhere.
Apply the TypeScript Strict Mode Standards rule to src/models/user.ts and src/utils/format.ts during review.
Review comments that remove redundant local annotations, add parameter and exported return types, replace any with unknown plus narrowing, rename IUser to User, suggest an as const object instead of a new enum, and pick one absent-value convention for the User model.
Cursor applies project rules stored as .mdc files in .cursor/rules. Each rule's frontmatter (description, globs, alwaysApply) decides whether it is always included, attached to matching files, chosen by the agent from its description, or only used when you @-mention it. Cursor also reads AGENTS.md in the project root and subdirectories for plain-Markdown instructions.
.cursor/rules/typescript-strict-mode-standards.mdcStarting point, not a tested configuration for your repository. Attached when files matching the globs are in context (alwaysApply: false with globs). Review the instructions, adapt the file paths and run your project's existing checks.
---
description: "TypeScript Strict Mode Standards project conventions"
globs: "**/*"
alwaysApply: false
---
Apply these standards to TypeScript code compiled with `strict` enabled.
## Principles
- Type safety: every value has a known type at compile time; values from outside the program (JSON, environment, user input) are validated before use.
- Catch errors at compile time: rely on `strict` and the extra checks the project enables, and keep `tsc --noEmit` passing in CI.
## Naming
- `camelCase` for variables and functions (`userName`, `getUserName`).
- `PascalCase` for classes, interfaces, type aliases and enums (`User`, `UserRole`). Do not prefix interfaces with `I`.
- `UPPER_SNAKE_CASE` only for true constants defined at module level.
## Annotate or infer
- Annotate function parameters, and the return types of exported functions and public methods (they document the contract and stop accidental changes).
- Let TypeScript infer local variables initialized with a value: `let userName = 'John Doe'` is inferred as `string`, not `any`.
- Variables declared without an initializer need an annotation (`let total: number;`). With `noImplicitAny`, parameters without a type are an error.
## any, unknown and assertions
- Do not use `any`. Use `unknown` for values of unknown shape and narrow them, or validate with a schema.
- Avoid `as` assertions and the non-null `!` operator; narrow instead. If one is unavoidable, add a comment explaining why it is safe. `as const` is fine.
## Interfaces, types and enums
- Use interfaces or type aliases for object shapes consistently; use type aliases for unions.
- Prefer string-literal unions or `as const` objects over `enum`. Enums generate runtime code and are rejected by `--erasableSyntaxOnly`, which matches what Node's built-in type stripping supports. Keep existing enums when changing them would be churn.
```ts
const Status = { Active: 'active', Disabled: 'disabled' } as const;
type Status = (typeof Status)[keyof typeof Status]; // 'active' | 'disabled'
```
## Absent values
- Choose one convention per boundary: optional properties (`name?: string`) for "may be missing", `null` for "explicitly empty" when an API or database uses it. Do not mix `null` and `undefined` for the same meaning.
## Type guards
- Narrow with `typeof`, `instanceof`, `in`, discriminant properties or user-defined guards before using a value of a union type.
## Code organization
- Use ES module `import`/`export` at the top of files; use `import type` for type-only imports.
- Group files by feature and keep names consistent with the existing project.
## Pitfalls
- `any` (explicit or through untyped libraries): add types or declare a typed wrapper.
- Unused variables and parameters: enable `noUnusedLocals`/`noUnusedParameters` or the equivalent lint rule.
- Unreachable code after `return`/`throw`.
- `as any` or double assertions (`as unknown as T`) to silence errors.
## Examples
```ts
// Inferred local: type is string
let userName = 'John Doe';
// Annotated exported function
export function getUserName(user: { name: string }): string {
return user.name;
}
// Interface with specific types
interface User {
name: string;
age: number;
}
// Instead of: interface User { name: any; age: number }
```
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
Cursor's docs recommend keeping individual rules under 500 lines and splitting large rules into composable ones. Cursor's documentation states that it supports AGENTS.md in the project root and in subdirectories. Use AGENTS.md for plain-Markdown instructions you also want other tools to read, and .mdc rules when you need Cursor's scoping options (globs, description-based selection, always-apply).
Documentation checked 2026-11-05: Cursor docs: Rules
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.