TypeScript Advanced Types
Iron Law: Before applying any type pattern, READ the actual TypeScript file first. Do not suggest type changes from memory — verify the actual code (file:line) before making any claim.
When to Use
- Building type-safe libraries or frameworks
- Creating reusable generic components
- Implementing complex type inference logic
- Designing type-safe API clients
- Building form validation systems
- Creating strongly-typed configuration objects
- Implementing type-safe state management
- Migrating JavaScript codebases to TypeScript
Do Not Use
- The task is unrelated to TypeScript advanced types
- You need a different domain or tool outside this scope (Flutter/Dart, Java, Python)
Core Type Patterns
1. Generics
Create reusable, type-flexible components while maintaining type safety. Use generic constraints (T extends HasLength) to narrow what types are accepted. Supports multiple type parameters, default types, and conditional constraints. See playbook §1 for examples.
2. Conditional Types
Create types that depend on conditions: T extends string ? true : false. Supports infer for extracting type components (e.g., return type, promise type, array element type). Distributive conditional types apply over union members automatically. See playbook §2 for examples.
3. Mapped Types
Transform existing types by iterating over their properties with [P in keyof T]. Supports modifiers (readonly, ?), key remapping via as, and filtering properties by type. Enables patterns like Partial<T>, Readonly<T>, and Getters<T>. See playbook §3 for examples.
4. Template Literal Types
Create string-based types with pattern matching: `on${Capitalize<EventName>}`. Enables type-safe event names, path builders, getter/setter name generation. Built-in helpers: Uppercase, Lowercase, Capitalize, Uncapitalize. See playbook §4 for examples.
5. Utility Types
TypeScript's built-in transformation utilities: Partial<T>, Required<T>, Readonly<T>, Pick<T,K>, Omit<T,K>, Exclude<T,U>, Extract<T,U>, NonNullable<T>, Record<K,T>. Combine with custom mapped types for DeepPartial<T> and DeepReadonly<T>. See playbook §5 and Pattern 4 for examples.
Tech Stack Applicability
| Stack |
Applicable |
Notes |
| Angular 21.x |
Yes |
Type-safe forms, component inputs, RxJS operator types, signal types |
| NestJS 11.x |
Yes |
Type-safe DTOs, guards, interceptors, Prisma query result types |
| MCP Builder (TypeScript) |
Yes |
Type-safe tool definitions, request/response contracts |
| Flutter / Dart |
No |
Dart has its own type system — use the flutter-mobile skill |
| Java / Spring Boot |
No |
Java generics differ significantly — use the java-spring-api skill |
| Python / FastAPI |
No |
Use Pydantic models via the python-dev skill |
Resources
resources/implementation-playbook.md contains full patterns, checklists, and code samples covering:
- Core Concepts: Generics, Conditional Types, Mapped Types, Template Literal Types, Utility Types
- Advanced Patterns:
- Pattern 1: Type-Safe Event Emitter
- Pattern 2: Type-Safe API Client
- Pattern 3: Builder Pattern with Type Safety
- Pattern 4: Deep Readonly / Deep Partial
- Pattern 5: Type-Safe Form Validation
- Pattern 6: Discriminated Unions
- Type Inference Techniques:
infer keyword, Type Guards, Assertion Functions
- Best Practices: 10 rules including
unknown over any, strict mode, type testing
- Common Pitfalls and Performance Considerations
Related Skills
| Combine With |
When |
nestjs-api |
Typing NestJS DTOs, guards, interceptors, Prisma result shapes with advanced utility types |
angular-spa |
Typing Angular reactive forms, component inputs/outputs, RxJS pipe operators |
mcp-builder |
Defining type-safe tool schemas, strongly-typed request/response contracts for MCP tools |
1---2name: typescript-advanced-types3description: Master TypeScript's advanced type system — generics, conditional types, mapped types, template literals, and utility types — for building type-safe Angular 21.x components, NestJS 11.x APIs, and MCP Builder integrations. Use when implementing complex type inference, type-safe API clients, branded types, DeepPartial/DeepReadonly patterns, or migrating JS to TS.4---56# TypeScript Advanced Types78> **Iron Law:** Before applying any type pattern, READ the actual TypeScript file first. Do not suggest type changes from memory — verify the actual code (file:line) before making any claim.910## When to Use1112- Building type-safe libraries or frameworks13- Creating reusable generic components14- Implementing complex type inference logic15- Designing type-safe API clients16- Building form validation systems17- Creating strongly-typed configuration objects18- Implementing type-safe state management19- Migrating JavaScript codebases to TypeScript2021## Do Not Use2223- The task is unrelated to TypeScript advanced types24- You need a different domain or tool outside this scope (Flutter/Dart, Java, Python)2526## Core Type Patterns2728### 1. Generics29Create reusable, type-flexible components while maintaining type safety. Use generic constraints (`T extends HasLength`) to narrow what types are accepted. Supports multiple type parameters, default types, and conditional constraints. See playbook §1 for examples.3031### 2. Conditional Types32Create types that depend on conditions: `T extends string ? true : false`. Supports `infer` for extracting type components (e.g., return type, promise type, array element type). Distributive conditional types apply over union members automatically. See playbook §2 for examples.3334### 3. Mapped Types35Transform existing types by iterating over their properties with `[P in keyof T]`. Supports modifiers (`readonly`, `?`), key remapping via `as`, and filtering properties by type. Enables patterns like `Partial<T>`, `Readonly<T>`, and `Getters<T>`. See playbook §3 for examples.3637### 4. Template Literal Types38Create string-based types with pattern matching: `` `on${Capitalize<EventName>}` ``. Enables type-safe event names, path builders, getter/setter name generation. Built-in helpers: `Uppercase`, `Lowercase`, `Capitalize`, `Uncapitalize`. See playbook §4 for examples.3940### 5. Utility Types41TypeScript's built-in transformation utilities: `Partial<T>`, `Required<T>`, `Readonly<T>`, `Pick<T,K>`, `Omit<T,K>`, `Exclude<T,U>`, `Extract<T,U>`, `NonNullable<T>`, `Record<K,T>`. Combine with custom mapped types for `DeepPartial<T>` and `DeepReadonly<T>`. See playbook §5 and Pattern 4 for examples.4243## Tech Stack Applicability4445| Stack | Applicable | Notes |46|-------|-----------|-------|47| Angular 21.x | Yes | Type-safe forms, component inputs, RxJS operator types, signal types |48| NestJS 11.x | Yes | Type-safe DTOs, guards, interceptors, Prisma query result types |49| MCP Builder (TypeScript) | Yes | Type-safe tool definitions, request/response contracts |50| Flutter / Dart | No | Dart has its own type system — use the flutter-mobile skill |51| Java / Spring Boot | No | Java generics differ significantly — use the java-spring-api skill |52| Python / FastAPI | No | Use Pydantic models via the python-dev skill |5354## Resources5556`resources/implementation-playbook.md` contains full patterns, checklists, and code samples covering:5758- **Core Concepts:** Generics, Conditional Types, Mapped Types, Template Literal Types, Utility Types59- **Advanced Patterns:**60 - Pattern 1: Type-Safe Event Emitter61 - Pattern 2: Type-Safe API Client62 - Pattern 3: Builder Pattern with Type Safety63 - Pattern 4: Deep Readonly / Deep Partial64 - Pattern 5: Type-Safe Form Validation65 - Pattern 6: Discriminated Unions66- **Type Inference Techniques:** `infer` keyword, Type Guards, Assertion Functions67- **Best Practices:** 10 rules including `unknown` over `any`, strict mode, type testing68- **Common Pitfalls and Performance Considerations**6970## Related Skills7172| Combine With | When |73|-------------|------|74| `nestjs-api` | Typing NestJS DTOs, guards, interceptors, Prisma result shapes with advanced utility types |75| `angular-spa` | Typing Angular reactive forms, component inputs/outputs, RxJS pipe operators |76| `mcp-builder` | Defining type-safe tool schemas, strongly-typed request/response contracts for MCP tools |