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

unexpected argument error type error when createStructuredCachedSelector provided with 2+ arguments #155

Open
rosesonfire opened this issue Dec 24, 2020 · 3 comments
Labels

Comments

@rosesonfire
Copy link

rosesonfire commented Dec 24, 2020

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');
@toomuchdesign
Copy link
Owner

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.

@rosesonfire
Copy link
Author

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

@toomuchdesign
Copy link
Owner

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

@toomuchdesign toomuchdesign changed the title Optional argument unexpected argument error type error when createStructuredCachedSelector provided with 2+ arguments Feb 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants