re-reselect icon indicating copy to clipboard operation
re-reselect copied to clipboard

`unexpected argument error` type error when `createStructuredCachedSelector` provided with 2+ arguments

Open rosesonfire opened this issue 4 years ago • 3 comments

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

  1. Create a structured cached selector with createStructuredCachedSelector with all its selectors having two parameters. The second parameter is optional for all of them.
  2. 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');

rosesonfire avatar Dec 24 '20 07:12 rosesonfire

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.

toomuchdesign avatar Dec 24 '20 10:12 toomuchdesign

Hi @toomuchdesign . Great catch! Do you think this can be prioritized? Thanks!

rosesonfire avatar Dec 24 '20 11:12 rosesonfire

I haven't found a quick fix, yet. Feel free to open a PR is you found one.

toomuchdesign avatar Dec 26 '20 12:12 toomuchdesign