how to use selectors in redux app with TypeScript?

SuperManEver picture SuperManEver · Feb 16, 2017 · Viewed 10.4k times · Source

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.

Answer

Radio- picture Radio- · Feb 16, 2017

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
);