Repository navigation
fix(store): fix inferrence of array types - #824
Closed
otonashixav wants to merge 1 commit into
Closed
otonashixav wants to merge 1 commit into
otonashixav wants to merge 1 commit into
Conversation
Previously produce would wrongly infer all arrays from a store as readonly. This fix makes the return type of produce StoreSetter instead, fixing the issue. This change has a few caveats; namely, produce is less strictly typed: it should return a function that takes and returns `DeepReadonly<T>`, but now it returns all types contained in `StoreSetter<T>`. This has no consequence for normal usage other than better type inference. Additionally to facilitate this change, DeepReadonly is no longer applied to the entire SetStoreFunction, but only where it is consumed (the setter and filter functions).
Pull Request Test Coverage Report for Build 1777586321
💛 - Coveralls |
trusktr
reviewed
Feb 6, 2022
| | T | ||
| | Partial<T> | ||
| | ((prevState: T, traversed?: (keyof any)[]) => Partial<T> | void); | ||
| | ((prevState: DeepReadonly<T>, traversed?: (keyof any)[]) => Partial<DeepReadonly<T>> | void); |
Contributor
There was a problem hiding this comment.
I haven't gotten to test inference here yet. But if there's a way to fix the broken inference you mentioned, while keeping the "correct" readonly types, that'd be nice, so as to prevent people doing the wrong thing.
Contributor
Author
There was a problem hiding this comment.
This PR changes two things and mostly keeps the readonly correctness:
- The types of
SetStoreFunctionandStoreSettersuch thatDeepReadonlyisn't applied directly at the root of the former, and instead only where the user would "interact" with the store in the latter. producenow accepts all types that are part of the union ofStoreSetterso that it can use theTtype included in that type to properly type its own parameters, avoiding the inference issue. It minimally had to have its signature changed to includeTwithout the rest of the union, but I thinkStoreSetteris more readable.
Do you mean that StoreSetter should also accept DeepReadonly variants of T | Partial<T>?
The alternative to this change would be a new DeepMutable type which marks all properties as mutable (-readonly), but that seems incorrect to me as the original type is available, and as it would also mark properties that were originally readonly (i.e. before being made into a store) as mutable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously produce would wrongly infer all arrays from a store as readonly.
This fix makes the return type of produce StoreSetter instead, fixing the issue.
This change has a few caveats; namely, produce is less strictly typed: it should return a function that takes and returns
DeepReadonly<T>, but now it returns all types contained inStoreSetter<T>. This has no consequence for normal usage other than better type inference.Additionally to facilitate this change, DeepReadonly is no longer applied to the entire SetStoreFunction, but only where it is consumed (the setter and filter functions).
#790 would also fix this issue.