Making Referential Stability a Type
5 days ago
- Introduces a phantom brand type `Stable<T>` to encode referential stability into TypeScript types, preventing accidental re-creation of objects, arrays, and functions.
- Provides a separate import `stableref/react` with stricter hook signatures that fail at the dependency list when stability is not proven, rather than falling back to permissive types.
- React guarantees stability for state, setState, and ref objects; these are automatically branded as `Stable`.
- Context values can be enforced as stable using `createStableContext`, ensuring memoization boundaries are honored.
- Preact has a parallel entry point `stableref/preact` with the same strict dependency checking.
- Limitations include explicit casts (`as Stable`) and the React Compiler, but the type-based approach complements automated memoization.
- Type errors are designed as actionable guardrails with descriptive messages, aiding both human developers and AI coding agents.
- The broader vision extends beyond memoization to carry proof-carrying properties across component boundaries and hooks.