Skip to content

Add immutability to models and raw data where appropriate #972

@sridmad

Description

@sridmad

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions