TypeScript
Overview
Strict TypeScript engineering standard. Enforces noImplicitAny, discriminated unions, branded types, immutability, exhaustive switch checks, and zero unsafe any or as unknown as T casts.
When to Use
Activate on all TypeScript and JavaScript codebases to ensure compile-time type safety, robust domain modeling, and foolproof function contracts.
Negative Constraints (What NOT to Do)
- NEVER use
any: Useunknownwith type guards, discriminated unions, or Zod schemas. - NEVER use type assertions (
as Typeoras unknown as Type) to bypass safety: Fix the underlying type signature or use runtime narrowing (instanceof,typeof,in). - NEVER use non-null assertions (
foo!.bar): Handlenullandundefinedwith optional chaining (?.) or explicit error guards. - NEVER export mutable global arrays or object constants: Always mark constant objects and arrays with
as constandreadonly. - NEVER omit explicit return types on exported functions: Exported public APIs must declare explicit return types to protect consumers.
Rules & Patterns
Strict Mode
Always use strict TypeScript configuration:
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
}
}
Types
- Prefer
interfacefor object shapes,typefor unions/intersections - No
any— useunknownif type is truly unknown, then narrow - Explicit return types for exported functions
- Const assertions —
as constfor literal types
// Good
interface User {
id: string;
name: string;
role: 'admin' | 'user';
}
// Unions
type Result<T> = { ok: true; data: T } | { ok: false; error: string };
Utility Types
Partial<T>— all properties optionalRequired<T>— all properties requiredPick<T, K>— select specific propertiesOmit<T, K>— remove specific propertiesRecord<K, V>— key-value map
Type Guards
function isUser(value: unknown): value is User {
return typeof value === 'object' && value !== null && 'id' in value;
}
Generic Patterns
// Repository pattern
interface Repository<T extends { id: string }> {
findById(id: string): Promise<T | null>;
create(data: Omit<T, 'id'>): Promise<T>;
update(id: string, data: Partial<T>): Promise<T>;
delete(id: string): Promise<void>;
}
Anti-Patterns
- [FAIL]
any— useunknown+ type guards - [FAIL] Type assertions (
as) — prefer type guards - [FAIL] Non-null assertions (
!) — handle null explicitly - [FAIL] Enums — prefer union types or
as constobjects - [FAIL] Complex generics without JSDoc — document intent
Code Examples
See EXAMPLES.md for detailed code examples.
Validation Checklist
What to verify during the review phase before completing the task.
Common Mistakes
Anti-patterns and things to explicitly avoid. See TROUBLESHOOTING.md.
Integration Notes
How this skill interacts with other skills.