fix(useSelector): add NoInfer to equality fn in UseSelector type (#2186)#2341
Open
veksa wants to merge 1 commit into
Open
fix(useSelector): add NoInfer to equality fn in UseSelector type (#2186)#2341veksa wants to merge 1 commit into
NoInfer to equality fn in UseSelector type (#2186)#2341veksa wants to merge 1 commit into
Conversation
commit: |
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.
Fixes #2186.
When you pass
shallowEqualas the second argument touseSelector(or to ahook made with
useSelector.withTypes<State>()), TypeScript was treating theequality function as an inference site for the selected value.
shallowEqualis typed with
anyparameters, so that could collapse the result down toanyinstead of the selector's actual return type.
The implementation inside
createSelectorHookalready wraps this argument inNoInfer— the publicUseSelectorinterface just never got the sametreatment. So this just brings the two in line:
<TState extends StateType = StateType, Selected = unknown>( selector: (state: TState) => Selected, - equalityFnOrOptions?: EqualityFn<Selected> | UseSelectorOptions<Selected>, + equalityFnOrOptions?: + | EqualityFn<NoInfer<Selected>> + | UseSelectorOptions<NoInfer<Selected>>, ): SelectedOne heads-up: on
masterwith the currently supported TS versions I couldn'tactually reproduce the
anyresult anymore — the existingshallowEqualtestpasses, and the new ones pass with or without this change. So really this is
(a) keeping the public type consistent with the implementation, and (b) a
safety net for older/other TS setups where the equality arg can still leak into
inference. If you'd rather just close the issue with the added tests and skip
the type change, I'm happy to drop it.
I also added type tests for the cases from the issue that weren't covered
before: a selector declared as a separate variable (not inline), the same
through plain
useSelector, and passingshallowEqualvia the options object.Type-only change, nothing runtime.