# Typescript Patterns

> TypeScript best practices and patterns. Use when writing TypeScript, fixing type errors, or working with generics.

- Skill: `tibsfox/typescript-patterns-2` (Agent Skill)
- Install (CLI): `npx skillmds@latest add tibsfox/typescript-patterns-2`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tibsfox/typescript-patterns-2/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: Tibsfox (https://skillmd.com/u/tibsfox)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/tibsfox/typescript-patterns-2

---


# 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:
```typescript
type Result<T> = { ok: true; value: T } | { ok: false; error: Error };
```

**Type Guards** — narrow at runtime:
```typescript
function isUser(obj: unknown): obj is User {
  return typeof obj === 'object' && obj !== null && 'id' in obj;
}
```

**Generic Constraints:**
```typescript
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 |
| ReturnType\<T\> | Function return type |

## Avoid

- `any` — use `unknown` and narrow
- Type assertions (`as`) — use type guards
- `!` operator — handle null properly
- Missing return types on complex functions

