TypeScript Pro
Core Workflow
- Analyze type architecture - Review tsconfig, type coverage, build performance
- Design type-first APIs - Create branded types, generics, utility types
- Implement with type safety - Write type guards, discriminated unions, conditional types
- Optimize build - Configure project references, incremental compilation
- Test types - Confirm type coverage, validate all public APIs have explicit return types
Key Patterns
Branded Types
type Brand<T, B extends string> = T & { readonly __brand: B };
type UserId = Brand<string, "UserId">;
type OrderId = Brand<number, "OrderId">;
const toUserId = (id: string): UserId => id as UserId;
const toOrderId = (id: number): OrderId => id as OrderId;
function getOrder(userId: UserId, orderId: OrderId) { /* ... */ }
Discriminated Unions & Type Guards
type LoadingState = { status: "loading" };
type SuccessState = { status: "success"; data: string[] };
type ErrorState = { status: "error"; error: Error };
type RequestState = LoadingState | SuccessState | ErrorState;
function isSuccess(state: RequestState): state is SuccessState {
return state.status === "success";
}
function renderState(state: RequestState): string {
switch (state.status) {
case "loading": return "Loading...";
case "success": return state.data.join(", ");
case "error": return state.error.message;
default: {
const _exhaustive: never = state;
throw new Error(`Unhandled state: ${_exhaustive}`);
}
}
}
Recommended tsconfig.json
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"exactOptionalPropertyTypes": true,
"isolatedModules": true,
"declaration": true,
"declarationMap": true,
"incremental": true,
"skipLibCheck": false
}
}
Constraints
MUST DO
- Enable strict mode with all compiler flags
- Use type-first API design
- Implement branded types for domain modeling
- Use
satisfies operator for type validation
- Create discriminated unions for state machines
- Generate declaration files for libraries
MUST NOT DO
- Use explicit
any without justification
- Skip type coverage for public APIs
- Disable strict null checks
- Use
as assertions without necessity
- Use enums (prefer const objects with
as const)
Knowledge Reference
TypeScript 5.0+, generics, conditional types, mapped types, template literal types, discriminated unions, type guards, branded types, tRPC, project references
1---2name: typescript-pro3description: Implements advanced TypeScript type systems, creates custom type guards, utility types, and branded types, and configures tRPC for end-to-end type safety.4license: MIT5compatibility: opencode6metadata:7 source: https://skillmd.com/skills/belentani7/typescript-pro8 author: open-source9 version: "1.1.0"10 domain: language11 triggers: TypeScript, generics, type safety, conditional types, mapped types, tRPC, tsconfig, type guards, discriminated unions12 role: specialist13 scope: implementation14 output-format: code15 related-skills: angular-architect, nextjs-developer, vue-expert16---1718# TypeScript Pro1920## Core Workflow21221. **Analyze type architecture** - Review tsconfig, type coverage, build performance232. **Design type-first APIs** - Create branded types, generics, utility types243. **Implement with type safety** - Write type guards, discriminated unions, conditional types254. **Optimize build** - Configure project references, incremental compilation265. **Test types** - Confirm type coverage, validate all public APIs have explicit return types2728## Key Patterns2930### Branded Types31```typescript32type Brand<T, B extends string> = T & { readonly __brand: B };33type UserId = Brand<string, "UserId">;34type OrderId = Brand<number, "OrderId">;3536const toUserId = (id: string): UserId => id as UserId;37const toOrderId = (id: number): OrderId => id as OrderId;3839function getOrder(userId: UserId, orderId: OrderId) { /* ... */ }40```4142### Discriminated Unions & Type Guards43```typescript44type LoadingState = { status: "loading" };45type SuccessState = { status: "success"; data: string[] };46type ErrorState = { status: "error"; error: Error };47type RequestState = LoadingState | SuccessState | ErrorState;4849function isSuccess(state: RequestState): state is SuccessState {50 return state.status === "success";51}5253function renderState(state: RequestState): string {54 switch (state.status) {55 case "loading": return "Loading...";56 case "success": return state.data.join(", ");57 case "error": return state.error.message;58 default: {59 const _exhaustive: never = state;60 throw new Error(`Unhandled state: ${_exhaustive}`);61 }62 }63}64```6566### Recommended tsconfig.json67```json68{69 "compilerOptions": {70 "target": "ES2022",71 "module": "NodeNext",72 "moduleResolution": "NodeNext",73 "strict": true,74 "noUncheckedIndexedAccess": true,75 "noImplicitOverride": true,76 "exactOptionalPropertyTypes": true,77 "isolatedModules": true,78 "declaration": true,79 "declarationMap": true,80 "incremental": true,81 "skipLibCheck": false82 }83}84```8586## Constraints8788### MUST DO89- Enable strict mode with all compiler flags90- Use type-first API design91- Implement branded types for domain modeling92- Use `satisfies` operator for type validation93- Create discriminated unions for state machines94- Generate declaration files for libraries9596### MUST NOT DO97- Use explicit `any` without justification98- Skip type coverage for public APIs99- Disable strict null checks100- Use `as` assertions without necessity101- Use enums (prefer const objects with `as const`)102103## Knowledge Reference104105TypeScript 5.0+, generics, conditional types, mapped types, template literal types, discriminated unions, type guards, branded types, tRPC, project references