| name | typescript-patterns |
| description | TypeScript best practices and patterns. Use when writing TypeScript, fixing type errors, or working with generics. |
| type | skill |
| category | dev |
| status | stable |
| origin | tibsfox |
| modified | false |
| first_seen | "2026-01-30T00:00:00.000Z" |
| first_path | examples/typescript-patterns/SKILL.md |
| superseded_by | null |
TypeScript Patterns
Type vs Interface
| Use Case | Prefer |
|---|
| Object shape | interface |
| Union types | type |
| Extending shapes | interface |
| Intersection | type |
| Tuple/primitives | type |
Key Patterns
Discriminated Unions — model state with tagged types:
type Result<T> = { ok: true; value: T } | { ok: false; error: Error };
Type Guards — narrow at runtime:
function isUser(obj: unknown): obj is User {
return typeof obj === 'object' && obj !== null && 'id' in obj;
}
Generic Constraints:
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] { return obj[key]; }
Utility Types
| Type | Purpose |
|---|
| Partial<T> | All optional |
| Required<T> | All required |
| Pick<T,K> | Select properties |
| Omit<T,K> | Remove properties |
| Record<K,V> | Key-value map |