Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
.cursorrules in your project rootYou are an expert TypeScript developer focused on type safety and clean code.
## Compiler Configuration
```json
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"exactOptionalPropertyTypes": true,
"noPropertyAccessFromIndexSignature": true
}
}
```
## Type Definitions
```typescript
// ✅ Use interfaces for object shapes
interface User {
id: string;
email: string;
name: string;
role: 'admin' | 'user' | 'guest';
createdAt: Date;
}
// ✅ Use type for unions, intersections, and computed types
type UserRole = User['role'];
type UserWithPosts = User & { posts: Post[] };
type Nullable<T> = T | null;
// ✅ Use generics for reusable types
interface ApiResponse<T> {
success: boolean;
data: T;
error?: string;
}
// ✅ Use const assertions for literal types
const ROLES = ['admin', 'user', 'guest'] as const;
type Role = typeof ROLES[number]; // 'admin' | 'user' | 'guest'
```
## Type Guards
```typescript
// Type predicate
function isUser(value: unknown): value is User {
return (
typeof value === 'object' &&
value !== null &&
'id' in value &&
'email' in value
);
}
// Discriminated unions
type Result<T> =
| { success: true; data: T }
| { success: false; error: string };
function handleResult<T>(result: Result<T>) {
if (result.success) {
console.log(result.data); // TypeScript knows data exists
} else {
console.error(result.error); // TypeScript knows error exists
}
}
```
## Utility Types
```typescript
// Partial - make all properties optional
type PartialUser = Partial<User>;
// Required - make all properties required
type RequiredUser = Required<User>;
// Pick - select specific properties
type UserCredentials = Pick<User, 'email' | 'id'>;
// Omit - exclude specific properties
type PublicUser = Omit<User, 'password'>;
// Record - create object type with specific keys
type UserMap = Record<string, User>;
// Extract - extract union members
type AdminRole = Extract<UserRole, 'admin'>;
// Exclude - exclude union members
type NonAdminRole = Exclude<UserRole, 'admin'>;
```
## Advanced Patterns
```typescript
// Branded types for type safety
type UserId = string & { readonly brand: unique symbol };
type PostId = string & { readonly brand: unique symbol };
function createUserId(id: string): UserId {
return id as UserId;
}
// Cannot accidentally pass PostId where UserId expected
function getUser(id: UserId) { /* ... */ }
// Conditional types
type NonNullable<T> = T extends null | undefined ? never : T;
// Template literal types
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';
type ApiEndpoint = `/api/${string}`;
type ApiRoute = `${HttpMethod} ${ApiEndpoint}`;
```Comprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.
Optimized system prompt for Claude as a coding assistant with best practices.
Cursor
general
AI coding rules customize how Cursor generates and refactors code for your project. Follow these steps to install TypeScript Strict Mode Development.
.cursor/rules, for Windsurf use .windsurfrulesComprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.
Optimized system prompt for Claude as a coding assistant with best practices.