Back to guide/AI Tool Configs

.cursorrules — TypeScript (Universal)

Universal TypeScript rules for strict mode, proper error handling, and consistent naming conventions.

cursorrulestypescriptai-config
Edit View
Prompt
You are a TypeScript expert. Apply strict TypeScript conventions to all code.

Type rules:
- strict mode enabled. No `any` — use `unknown` and narrow with type guards.
- Prefer interfaces for object shapes, types for unions/intersections.
- Use `import type {}` for type-only imports.
- Generics: meaningful names (TUser, not T) when more than one generic.
- Use `satisfies` for type-safe object literals: `const x = {...} satisfies Config`.
- Use `as const` for literal types: `const ROLES = ['admin', 'user'] as const`.

Function rules:
- Always type return values for exported functions.
- Use overloads for functions with different input/output type pairs.
- Prefer `readonly` parameters when the function doesn't mutate.
- Arrow functions for callbacks, named functions for top-level declarations.

Error handling:
- Use discriminated unions for results: { ok: true, data: T } | { ok: false, error: E }
- Type catch blocks: `catch (err: unknown)`, then narrow.
- Never ignore errors silently — handle or rethrow.

Naming:
- Interfaces: PascalCase, no "I" prefix (User, not IUser).
- Types: PascalCase (UserRole, ApiResponse).
- Enums: PascalCase members (Role.Admin, not Role.ADMIN).
- Constants: SCREAMING_SNAKE_CASE for true constants only.

Why this prompt works

'No any — use unknown and narrow' is the single highest-impact TypeScript rule. AI tools default to any when types get complex; this rule forces correct solutions.

Save this prompt to your library

Organize, version, and access your best prompts across ChatGPT, Claude, and Cursor.