When to Use
Load this skill when:
- Writing new TypeScript code.
- Defining models, interfaces, or types.
- Cleaning up untyped or loosely typed code.
Critical Patterns
1. Strict Typing
"If it compiles, it must be correct".
| Pattern | Rule | Why |
|---|---|---|
any |
FORBIDDEN | Use unknown and type guards instead. |
| Returns | Explicitly declare | Improves readability and prevents logic errors. |
| Null Checks | Strict | Force the handling of null/undefined. |
2. Data Structures
Prefer modern, lean type definitions.
| Use Case | Pattern |
|---|---|
| Enums | ❌ Prohibited. Use const assertions or Union Types. |
| Immutability | Use readonly for arrays and object properties. |
| Validation | Use Zod for runtime validation. |
Code Examples
Example: Proper Union Types
type Role = 'Admin' | 'User';
const ROLES = {
ADMIN: 'Admin',
USER: 'User'
} as const;
Anti-Patterns
Don't: Non-Null Assertions (!)
Why: It lies to the compiler and causes runtime crashes.
// ❌ const user = users.find(u => u.id === id)!;
Don't: Interface Prefixes
Why: Redundant and outdated.
// ❌ interface IUser { ... }
Quick Reference
- No Enums: Only Union Types or Const Assertions.
- Strict:
strict: trueintsconfig.json. - Naming: Use PascalCase for types/interfaces.