redux-thunk with typescript

三世轮回 提交于 2019-12-10 16:48:29

问题


I am trying to learn redux and I am trying to implement the redux-thunk middleware. I've been following a few different tutorials and they suggest something similar to this:

import thunk from "redux-thunk";
import promise from "redux-promise-middleware";
...
const middleware = applyMiddleware(promise(), thunk);
const store = createStore(reducers, middleware);

This gives me the following error:

/Users/me/Documents/workspace/redux/node_modules/redux-thunk/index.d.ts (4,47): Generic type 'Dispatch' requires 2 type argument(s).

Can someone please explain what is going on and how to fix this?

Many thanks


回答1:


I think you need to type dispatch in a way:

interface IStoreState {
  readonly pendingActions: number;
  readonly isAuthenticated: boolean;
};

function signIn(): (dispatch: Dispatch<IStoreState>) =>  ....

Related article.




回答2:


This problem turns out to be related to the new version of redux (4.0.0) being incompatible with the current version of redux-thunk (2.2.0).

See this link: https://github.com/gaearon/redux-thunk/issues/169

and this PR: https://github.com/gaearon/redux-thunk/pull/180



来源:https://stackoverflow.com/questions/49907244/redux-thunk-with-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!