Proposal
Enforce immutability on model and raw data structures where appropriate by using TypeScript's readonly and Object.freeze. This will help prevent accidental mutations, ensure safer sharing of data, and reduce hard-to-track bugs.
Rationale
- Many model and raw objects are mutable by default, risking unintended changes deep in the object graph
- Marking data as readonly (and freezing where possible) will signal safe usage and catch errors at compile-time
Example Approach
export class DataModelBase<T> {
private _raw: Readonly<T>;
public get raw(): Readonly<T> {
return this._raw;
}
protected updateRaw(newRaw: T): void {
this._raw = Object.freeze({ ...newRaw });
}
}
Tasks
- Audit all model and raw data definitions
- Apply
readonly modifiers at type and property level as appropriate
- Use Object.freeze for runtime immutability in model constructors
- Add or update tests to verify that accidental mutation throws errors or fails type checks
Benefits
- Reduce accidental mutation
- More robust and predictable models
- Safer code, especially in shared and async contexts
Labels: refactoring, immutability, code quality
Proposal
Enforce immutability on model and raw data structures where appropriate by using TypeScript's
readonlyandObject.freeze. This will help prevent accidental mutations, ensure safer sharing of data, and reduce hard-to-track bugs.Rationale
Example Approach
Tasks
readonlymodifiers at type and property level as appropriateBenefits
Labels:
refactoring,immutability,code quality