Goal: types that make illegal states unrepresentable without ceremony.
Use for:
- modeling variant data and state machines
- typing flexible component or function APIs
- replacing
anyand unsafe casts with precise types
Workflow:
- Model variants as a discriminated union with a tag field.
- Narrow with the tag so each branch is fully typed.
- Use conditional types to derive types from inputs.
- Use template literal types for structured string keys.
- Reach for utility types (Pick, Omit, Record, ReturnType) first.
- Add generic constraints to keep inference helpful.
Patterns:
- discriminated unions over optional-flag soup
inferto extract types from structures- mapped + template literal types for key transforms
satisfiesto check without widening
Rules:
- prefer inference; annotate only where it adds safety or clarity
- avoid
any; useunknownthen narrow - do not over-engineer types past what the API needs
- a type that confuses readers is a liability, not a win