State Management
Overview
Patterns for managing state immutably in TypeScript. All state transitions should produce new values rather than mutating existing ones, keeping side effects predictable and functions pure. These rules apply to any stateful module in the monorepo.
Rules
Create New State Instead of Mutating
State should never be mutated in place. Return new arrays and objects from every transformation.
Correct
Incorrect
Encapsulate State with Factories
Use factories and closures to encapsulate state. Never use classes. Mutation inside a closure is the accepted pattern for stateful modules — the public API should remain immutable.
Correct
Incorrect
Derive State, Don't Store Duplicates
Compute derived values from source state on demand. Never store values that can be calculated from existing state.
Correct
Incorrect
References
- Design Patterns -- Factories and functional design
- Functions -- Pure functions