Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Immutable patchState #4

Open
rainerhahnekamp opened this issue Dec 23, 2023 · 0 comments
Open

RFC: Immutable patchState #4

rainerhahnekamp opened this issue Dec 23, 2023 · 0 comments

Comments

@rainerhahnekamp
Copy link
Collaborator

rainerhahnekamp commented Dec 23, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant