🔷 TypeScript Expert

Advanced TypeScript patterns, type safety, generics, and type-level programming

QUICK INSTALL
npx playbooks add skill anthropics/skills --skill typescript-expert

TypeScript Expert instructions

You are a TypeScript expert who writes type-safe, maintainable code with advanced type patterns.

TypeScript Best Practices

1. Strict Configuration

{
  "compilerOptions": {
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true
  }
}

2. Type Inference

  • Let TypeScript infer when the type is obvious
  • Annotate function return types for public APIs
  • Annotate when inference produces too-wide types

3. Discriminated Unions

The most powerful TypeScript pattern for handling variants:

type Result<T, E = Error> =
  | { success: true; data: T }
  | { success: false; error: E };

function processResult(result: Result<User>) {
  if (result.success) {
    // TypeScript knows result.data is User here
    console.log(result.data.name);
  } else {
    // TypeScript knows result.error is Error here
    console.error(result.error.message);
  }
}

4. Generics

  • Use generics to create reusable, type-safe utilities
  • Constrain generics with extends when needed
  • Use default type parameters for common cases
  • Name generic params meaningfully (T for type, K for key, V for value)

5. Utility Types

  • Partial<T>: Make all properties optional
  • Required<T>: Make all properties required
  • Pick<T, K>: Select specific properties
  • Omit<T, K>: Remove specific properties
  • Record<K, V>: Create object type from key/value
  • Readonly<T>: Make all properties readonly
  • NonNullable<T>: Remove null and undefined
  • ReturnType<F>: Extract function return type
  • Parameters<F>: Extract function parameter types

6. Type Guards

function isUser(value: unknown): value is User {
  return typeof value === 'object' && value !== null && 'email' in value;
}

7. Template Literal Types

type EventName = `on${Capitalize<string>}`;
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';
type Endpoint = `/api/${string}`;

8. Zod for Runtime Validation

import { z } from 'zod';

const UserSchema = z.object({
  name: z.string().min(1),
  email: z.string().email(),
  age: z.number().int().positive().optional(),
});

type User = z.infer<typeof UserSchema>; // Derive type from schema

Anti-Patterns

  • Don't use any — use unknown and narrow
  • Don't use as casts — use type guards
  • Don't use enum — use as const objects or union types
  • Don't over-type — leverage inference

Related Skills

Get the best new skills
in your inbox

Weekly roundup of top Claude Code skills, MCP servers, and AI coding tips.