| name | typescript |
| description | Apply for any TypeScript project. Covers: strict config, type patterns, generics, discriminated unions, type guards, utility types, and common anti-patterns. Trigger for: TypeScript, TS, types, interfaces, generics. |
TYPESCRIPT — Strict Mode Production Patterns
tsconfig — Strict baseline (non-negotiable)
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"target": "ES2022",
"moduleResolution": "bundler"
}
}
Type vs Interface
interface User { id: string; email: string }
interface AdminUser extends User { role: 'admin' }
type Status = 'pending' | 'running' | 'done' | 'failed'
type ApiResponse<T> = { data: T; timestamp: number } | { error: string }
Discriminated Unions — Replace enums
type JobResult =
| { status: 'success'; output: string; duration: number }
| { status: 'failed'; error: string; attempt: number }
| { status: 'pending' }
function handleResult(result: JobResult) {
switch (result.status) {
case 'success': return process(result.output)
case 'failed': return retry(result.attempt)
case 'pending': return wait()
}
}
Generics — When and how
interface Repository<T extends { id: string }> {
findById(id: string): Promise<T | null>
save(entity: T): Promise<T>
delete(id: string): Promise<void>
}
function first<T extends readonly unknown[]>(arr: T): T[0] | undefined {
return arr[0]
}
Type Guards
function isUser(value: unknown): value is User {
return typeof value === 'object' && value !== null &&
'id' in value && typeof (value as User).id === 'string'
}
const raw = JSON.parse(data)
if (isUser(raw)) { }
Utility Types — Know these
Partial<User>
Required<User>
Pick<User, 'id'|'email'>
Omit<User, 'password'>
Record<string, number>
Readonly<User>
ReturnType<typeof fetchUser>
Parameters<typeof submitJob>
Forbidden
❌ any — use unknown + type guard instead
❌ as casting without guard — use satisfies or guard
❌ ! non-null assertion without certainty
❌ Enums (runtime overhead) — use const objects or discriminated unions
❌ // @ts-ignore — fix the type instead
❌ Function type — always specify signature