Typescript Advanced Types

Use when: model complex APIs with discriminated unions, conditional, and template literal types.

kimtth ea52da0 1.1 KB Updated

File contents

Goal: types that make illegal states unrepresentable without ceremony.

Use for:

  • modeling variant data and state machines
  • typing flexible component or function APIs
  • replacing any and unsafe casts with precise types

Workflow:

  1. Model variants as a discriminated union with a tag field.
  2. Narrow with the tag so each branch is fully typed.
  3. Use conditional types to derive types from inputs.
  4. Use template literal types for structured string keys.
  5. Reach for utility types (Pick, Omit, Record, ReturnType) first.
  6. Add generic constraints to keep inference helpful.

Patterns:

  • discriminated unions over optional-flag soup
  • infer to extract types from structures
  • mapped + template literal types for key transforms
  • satisfies to check without widening

Rules:

  • prefer inference; annotate only where it adds safety or clarity
  • avoid any; use unknown then narrow
  • do not over-engineer types past what the API needs
  • a type that confuses readers is a liability, not a win

kimtth/agent-skill-100-lines-or-less/tree/main/skills/typescript-advanced-types commit ea52da0ac7

Frequently asked questions

npx skillmds@latest add kimtth/typescript-advanced-types