Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Type inference for reselect in containers #48

Closed
marcoturi opened this issue Jan 22, 2018 · 2 comments
Closed

Type inference for reselect in containers #48

marcoturi opened this issue Jan 22, 2018 · 2 comments

Comments

@marcoturi
Copy link

marcoturi commented Jan 22, 2018

Hi,
when declaring my containers props i do something like this:

// In my selector file
const simpleSelector = (state: RootState): string => state.mydata;
const reselectSelector = createSelector([simpleSelector], (selector: string): string => selector);

// In my container file
interface SimpleContainerProps {
  simpleSelector: typeof simpleSelector; // this is ok because type is string
  reselectSelector: typeof reselectSelector; // this gives problems because type is  'OutputSelector<RootState, string, (selector: string) => string>'
}

This gives me the opportunity to be sure that if I change a selector used by any components on my app, typescript compiler will throw an error. However as you can see from my code's comments I cant use it for selectors created by reselect using createSelector.

Does anyone encountered this problem too?

@marcoturi
Copy link
Author

Digging to this issue I found that is a common problem in typescript, and even my first example (typeof simpleSelector) doesn't work, as the type is not string but (state: RootState) => string.

Here are same reference:
https://stackoverflow.com/questions/40590034/typescript-typeof-function-return-value
microsoft/TypeScript#14400
https://github.com/kube/returnof

@piotrwitek
Copy link
Owner

yes this is a limitation of TypeScript type system, I could only recommend this workaround which is similar to the returnof that you mentioned: https://github.com/piotrwitek/utility-types#call

It is also used here in the guide to get Actions union type: https://github.com/piotrwitek/react-redux-typescript-guide#rootaction---union-type-of-all-action-objects

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

No branches or pull requests

2 participants