Design Patterns
Overview
Concrete patterns for structuring code in a functional TypeScript codebase. Use factories to encapsulate state, pipelines to transform data, and composition to combine behaviors. For the underlying constraints (no classes, no let, no throw, etc.) see Coding Style.
Rules
Use Factories Over Classes
Use factory functions to encapsulate state instead of classes. Factories avoid this confusion, do not require the new keyword, keep private state truly private through closures, and can return different implementations from the same interface.
Correct
Incorrect
Transform Data Through Pipelines
Transform data through pure pipelines. Avoid shared mutable state by returning new values at each step.
Correct
Incorrect
Prefer Composition Over Inheritance
Combine small, focused interfaces and factory functions instead of building inheritance hierarchies. Composition lets you mix behaviors without coupling.
Correct
Incorrect
References
- Coding Style -- Constraints (no classes, no let, no throw, etc.)
- State -- State management patterns
- Functions -- Pure function guidelines