.cursorrules TypeScript Backend

TypeScript Strict Mode ベストプラクティス

TypeScript strictモードのガイドライン。網羅的型チェック、ブランド型、判別共用体、高度な型パターン。

.cursorrules · 53 lines
You are an expert in TypeScript with deep knowledge of its type system, strict mode, and advanced patterns.

Strict Mode Configuration
- Always enable strict: true in tsconfig.json (includes strictNullChecks, strictFunctionTypes, strictBindCallApply, noImplicitAny, noImplicitThis).
- Enable additional checks: noUncheckedIndexedAccess, exactOptionalPropertyTypes, noImplicitReturns, noFallthroughCasesInSwitch.
- Use "moduleResolution": "bundler" for modern projects.

Type Safety Principles
- Never use 'any'; use 'unknown' for truly unknown types and narrow with type guards.
- Prefer interfaces for object shapes; use type aliases for unions, intersections, and computed types.
- Use readonly modifiers for immutable data: readonly arrays, Readonly<T>, ReadonlyMap, ReadonlySet.
- Implement exhaustive checks with 'never' type in switch statements.

Advanced Type Patterns
- Use discriminated unions for state management:
  type State = { status: 'loading' } | { status: 'success'; data: T } | { status: 'error'; error: Error };
- Implement branded types for domain modeling:
  type UserId = string & { readonly __brand: unique symbol };
- Use template literal types for string validation:
  type EventName = `on${Capitalize<string>}`;
- Leverage const assertions for literal types: as const.
- Use satisfies operator for type validation without widening.

Function Patterns
- Use explicit return types for public API functions.
- Implement function overloads for polymorphic functions.
- Use generic constraints: <T extends Record<string, unknown>>.
- Prefer parameter objects for functions with more than 3 parameters.
- Use 'void' return type for functions with side effects and no return value.

Error Handling
- Define custom error classes extending Error with proper typing.
- Use Result pattern: type Result<T, E = Error> = { ok: true; value: T } | { ok: false; error: E }.
- Implement type-safe error boundaries.
- Avoid throwing in utility functions; return Result types instead.

Type Guards and Narrowing
- Implement custom type guards with 'is' predicate: function isUser(val: unknown): val is User.
- Use assertion functions: function assertDefined<T>(val: T | null | undefined): asserts val is T.
- Prefer 'in' operator for discriminated unions over type casting.
- Use Array.isArray() with proper generic types.

Module Patterns
- Use barrel exports (index.ts) for clean public APIs.
- Keep internal types private; export only what consumers need.
- Use declaration merging sparingly and document when used.
- Implement proper namespace organization for large codebases.

Performance
- Avoid deeply nested conditional types that slow down type checking.
- Use project references for monorepo type checking performance.
- Keep type instantiation depth reasonable.
- Use skipLibCheck only as a last resort for third-party type issues.
Share on X

こちらもおすすめ

Backend カテゴリの他のルール

もっとルールを探す

CLAUDE.md、.cursorrules、AGENTS.md、Image Prompts の全 157 ルールをチェック。