angular-architects / ngrx-toolkit

Various Extensions for the NgRx Signal Store
MIT License
104 stars 17 forks source link

RFC: Immutable patchState #4

Open rainerhahnekamp opened 6 months ago

rainerhahnekamp commented 6 months ago

What about a patchState which (almost) guarantees immutability?

We might also provide an overloaded version, where the action name can be provided.

I see two options:

Option 1: Deep Readonly

The state, provided via patchStatecould be a nested readonly type.

If the state is

interface FlightState {
  flights: Flight[];
  airline: {
    name: string,
    shortcut: string
  },
  loaded: boolean;
  selectedId: number;
}

Following calls of patchState would fail to compile:

patchState(state, value => value.loaded = false);
patchState(state, value => value.airline.name = 'AUA');
patchState(state, value => value.flights[0].id = 1);
patchState(state, value => value.flights.push({id: 1}));

Option 2: Immer

The other option is to use immer.