Search code examples
typescriptreduxreselect

how to use selectors in redux app with TypeScript?


I try to use selectors from reselect library in my Redux app.

my selectors file looks:

import { createSelector } from 'reselect'

const postsSelectors = state => state.global.posts.allPostse;

export const getPosts = createSelector(
  [ postsSelectors ],
  (posts) => posts
);

and then I try to use in my component, like this:

const mapStateToProps = (state) => ({
  posts: getPosts(state),
});

When I try to compile all of this, I got this error:

enter image description here

I'm guessing that with how I declare types for props, which currently looks like that:

interface Props{
 posts(state: any): any
 loadStories(): void;
};

Help me please resolve this issue. Thanks in advance.


Solution

  • TypeScript is not expecting an array for the first argument. Just pass in your selector functions as arguments to createSelector, as in

    export const getPosts = createSelector(
      postsSelectors,
      (posts) => posts
    );