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

createSelector should return MemoizedSelector<State, Result, ProjectorFn> with fully-typed ProjectorFn to assist in unit-testing selectors #3196

Closed
MaximSagan opened this issue Oct 22, 2021 · 1 comment

Comments

@MaximSagan
Copy link

The projector argument (last argument) of createSelector is a pure function that uses the resultant values of the other selectors passed to define the result of a the new selector. It is available as the MemoizedSelector's project member, and this should be ideal for unit-testing selectors.

However, in all current signatures of createSelector, the ProjectorFn type argument of the return type MemoizedSelector is unspecified, meaning it defaults to DefaultProjectorFn<Result>, or (...args: any[]) => Result. It's nice that the Result type is available, but without the argument types, the unit tests become a little harder to write (at least for those of us who like to use Typescript to help guide their unit tests).

Luckily, this issue can be easily solved by simply providing the full projector type in createSelector's return type, e.g.

Before:

declare function createSelector<State, S1, S2, Result>(
    s1: Selector<State, S1>,
    s2: Selector<State, S2>,
    projector: (s1: S1, s2: S2) => Result
): MemoizedSelector<State, Result>;

After:

declare function createSelector<State, S1, S2, Result>(
    s1: Selector<State, S1>,
    s2: Selector<State, S2>,
    projector: (s1: S1, s2: S2) => Result
): MemoizedSelector<State, Result, (s1: S1, s2: S2) => Result>;

Please see this Typescript playground link for a worker example of the additional type safety that this change would provide.

If accepted, I would be willing to submit a PR for this feature

[x] Yes (Preferably after #3023 is merged)
[ ] No

@MaximSagan MaximSagan changed the title createSelector should return MemoizedSelector<State, Result, ProjectorFn> with fully-typed ProjectorFn to assist in unit testing selectors createSelector should return MemoizedSelector<State, Result, ProjectorFn> with fully-typed ProjectorFn to assist in unit-testing selectors Oct 22, 2021
@brandonroberts
Copy link
Member

Thanks @MaximSagan. Closing as a duplicate of #3097

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants