re-reselect
re-reselect copied to clipboard
`unexpected argument error` type error when `createStructuredCachedSelector` provided with 2+ arguments
Do you want to request a feature or report a bug?
bug
What is the current behaviour?
When a selectors of a structured cached selector has an optional argument, typescript gives "unexpected argument error" when the structured cached selector is called with that optional argument.
What is the expected behaviour?
TypeScript should not give error (I think)
Steps to Reproduce the Problem
- Create a structured cached selector with
createStructuredCachedSelector
with all its selectors having two parameters. The second parameter is optional for all of them. - Typescript gives "unexpected argument error" error when trying to pass second argument to the structured cached selector.
Specifications
- Version: 4.0.0
- Platform: Ubuntu 16.04, NodeJS 15.2.0
Example:
import { createStructuredCachedSelector } from 're-reselect';
type State = {
itemCounts: {
[key: string]: number,
}
};
const itemCountSelector = createStructuredCachedSelector({
allItemCounts: (state: State) => state,
// Notice that the second argument is optional
singleItemCount: (state: State, itemId?: string) => (itemId ? state.itemCounts[itemId] : null),
})(
(_state, itemId?: string) => itemId,
);
const state: State = {
itemCounts: {
item1: 2,
item2: 2,
},
};
// This does not give error
itemCountSelector(state);
// This gives an error - "Expected 1 arguments, but got 2"
itemCountSelector(state, 'item1');
Hi @rosesonfire, thanks reporting.
It seems to be caused by TS definition overloads happening here. I guess it's missing a specific overload covering the case of a first optional argument.
Hi @toomuchdesign . Great catch! Do you think this can be prioritized? Thanks!
I haven't found a quick fix, yet. Feel free to open a PR is you found one.