Home › TypeScript › TypeScript strict mode rules

TypeScript strict mode rules

Cursor rules for strict TypeScript 5.9+: no any, unknown narrowing, discriminated unions, satisfies, and runtime validation.

CursorTypeScript

How to use

Save this as .cursor/rules/typescript.mdc in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

Note: Review and test before use. Provided as is, without warranty. See Terms.

.cursor/rules/typescript.mdcDownload  
---
description: TypeScript strict mode conventions
globs: ["**/*.{ts,tsx,mts,cts}"]
alwaysApply: false
---

# Strict TypeScript

- Keep `strict: true`, `noUncheckedIndexedAccess`, `noImplicitOverride`, and `exactOptionalPropertyTypes` (if enabled). Never loosen `tsconfig.json` to fix an error.
- No `any`. External data is `unknown` until validated with a schema (zod/valibot) at the boundary; derive types with `z.infer`.
- No non-null assertions (`!`) or `as` casts to silence errors. Narrow with checks, `in`, `instanceof`, or type guards (`function isUser(x: unknown): x is User`). `as const` and `satisfies` are fine.
- Model states as discriminated unions (`{ status: 'loading' } | { status: 'ok'; data: T } | { status: 'error'; error: string }`) and switch exhaustively with a `never` check.
- Prefer `type` aliases for unions and object shapes; `interface` for extendable public contracts. Be consistent with the file.
- Use `readonly` arrays/properties for data that shouldn't change; `Readonly<T>` for params.
- Return types explicit on exported functions.
- Errors: throw `Error` subclasses; in `catch (e)` treat `e` as `unknown`.
- Utility types (`Pick`, `Omit`, `Partial`, `Record`) over duplicating shapes; avoid deeply clever conditional types in app code.
- No `// @ts-ignore`; if unavoidable, `// @ts-expect-error <reason>`.
- `import type` for type-only imports (`verbatimModuleSyntax`).

## Done when
`npx tsc --noEmit` reports zero errors and lint passes.

More: all TypeScript rules · all Cursor rules

Related rules

TypeScript npm library AGENTS.md

AGENTS.md for publishing TypeScript npm packages: ESM exports maps, bundling with tsdown, type checks, and changesets.

AGENTS.mdTypeScript

Astro content site rules

Cursor rules for Astro 5 sites with content collections, islands, and zero-JS-by-default pages.

CursorAstro

Chrome extension (Manifest V3) rules

Cursor rules for building Manifest V3 browser extensions with TypeScript and minimal permissions.

CursorChrome extensions

Lit web components rules

Cursor rules for Lit 3 web components in TypeScript: reactive properties, templates, styles, events, and accessibility.

CursorLit / web components

Next.js 16 rules (Cache Components, proxy.ts)

Cursor rules for Next.js 16 App Router: Cache Components with "use cache", proxy.ts, async request APIs, Turbopack.

CursorNext.js

Next.js App Router (TypeScript) rules

Cursor project rules for Next.js 15 App Router apps with TypeScript, Server Components, and Tailwind.

CursorNext.js

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.