Lesson 17Memory
Modular Rules
Path-specific instructions
What are Modular Rules
Modular rules are separate .md files in the .claude/rules/ directory that apply only to specific files.
.claude/rules/ ├── typescript.md # Rules for *.ts, *.tsx ├── react.md # Rules for components ├── api.md # Rules for API routes ├── tests.md # Rules for tests ├── security.md # Security guidelines └── database.md # Database conventions
💡 Rules are automatically loaded when Claude works with files matching the patterns.
Rule Format
Each rule is a Markdown file with YAML frontmatter containing paths:
--- paths: - "**/*.ts" - "**/*.tsx" - "!**/*.test.ts" --- # TypeScript Rules Your instructions here...
Include patterns
**/*.ts— all TS filessrc/components/**— all in components*.config.js— configs at root
Exclude patterns
!**/*.test.ts— except tests!**/node_modules/**— except modules!dist/**— except dist
Example Rules
typescript.md**/*.ts**/*.tsx
--- paths: - "**/*.ts" - "**/*.tsx" --- # TypeScript Rules - Use strict TypeScript settings - Prefer interfaces over types for objects - Always add return types to functions - Use 'unknown' instead of 'any'
react.mdsrc/components/**/*.tsx
---
paths:
- "src/components/**/*.tsx"
---
# React Component Rules
- Use functional components only
- Props interface should be named {ComponentName}Props
- Use memo() for expensive renders
- Prefer composition over prop drillingapi.mdsrc/app/api/**/*.ts
--- paths: - "src/app/api/**/*.ts" --- # API Route Rules - Always validate request body with Zod - Return proper HTTP status codes - Handle errors with try/catch - Log all errors to monitoring
tests.md**/*.test.ts**/*.spec.ts
--- paths: - "**/*.test.ts" - "**/*.spec.ts" --- # Testing Rules - Use describe/it blocks - Mock external dependencies - Test edge cases - Aim for 80% coverage
When Rules Apply
Claude reads a file → Rules for this file type are loaded
Claude edits a file → Rules are applied when generating code
Claude creates a new file → Rules determined by extension/path
Best Practices
- 1.Create separate rules for different file types
- 2.Use exclusions (!) for tests and generated files
- 3.Document "why", not just "what"
- 4.Commit rules to git with the project
This lesson is part of a structured LLM course.
My Learning Path