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

feat: remove Immutable utility type from core interfaces #42

Merged
merged 2 commits into from
Nov 7, 2023

Conversation

cefn
Copy link
Owner

@cefn cefn commented Nov 5, 2023

This substantial change in signature eliminates a core dependency on Immutable, with the aim that Immutable is elective by the creators of a store. It's still strongly recommended, (but not enforced).

This is important if the immutable operations you are using on your store state come from a world without compile-time immutability. Even though they in practice never make changes, APIs often declare e.g. doTheThing<Arr extends number[]>(entries:Arr) not doTheThing<Arr extends readonly number[]>(entries:Arr) which would be required for it to accept an Immutable<number[]>.

However, operating WITHOUT Immutable should be done with care, and because you prefer THAT hassle (incautious mutation of state which shouldn't be mutated) versus the hassle of readonly markers throughout your data.

So instead of their being magic to transform your State into an Immutable<State>, with all reads and selected values being wrapped in Immutable<> with a signature like this...

export type CounterState = {
  counter: number;
};

export const store = createStore<CounterState>({
  counter: 0,
})

You opt-in explicitly, which has no effect on any calls YOU make, but radically simplifies the type gymnastics for special cases since Immutable is in the original type you specify, rather than needing selective transforms as read/write boundaries are crossed.

export type CounterState = Immutable<{
  counter: number;
}>;

export const store = createStore<CounterState>({
  counter: 0,
})

Potentially there could be a utility type to simplify opting into immutability to embody the recommendation, like...

export const store = createImmutableStore({some:"value"})

@cefn cefn force-pushed the remove-core-immutable branch 2 times, most recently from 34b30b4 to 8be6236 Compare November 5, 2023 02:12
@cefn cefn changed the title feat: remove Immutable utility type feat: remove Immutable utility type from core interfaces Nov 6, 2023
@cefn cefn force-pushed the remove-core-immutable branch 3 times, most recently from 58dfaac to 886255d Compare November 7, 2023 15:27
@cefn cefn merged commit 0da58e5 into main Nov 7, 2023
2 checks passed
@cefn cefn deleted the remove-core-immutable branch November 7, 2023 16:35
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

Successfully merging this pull request may close these issues.

1 participant