我正在尝试使用next-redux-wrapper存储从Next.js中的getServerSideProps分派一个thunk。但是,我一直收到以下typescript错误:
TS2345: Argument of type '(dispatch: any) => Promise<any>' is not assignable to parameter of type 'AnyAction'. Property 'type' is missing in type '(dispatch: any) => Promise<any>' but required in type 'AnyAction'.我以前没有在Redux中使用过TypeScript,所以不确定我在这里做错了什么……
我的代码如下:
// page.tsx
export const getServerSideProps = wrapper.getServerSideProps(
async ({ store, params }) => {
store.dispatch(myThunkFunction(params.id));
return {
props: {
id: params.id,
},
};
});// thunks.ts
export const myThunkFunction = id => {
return async dispatch => {
...
};
};发布于 2020-10-02 07:53:27
It seems to be a known issue with next-redux-wrapper when using redux-thunk,其中next-redux-wrapper在getServerSideProps中返回的dispatch类型(或者我正在使用的getInitialProps )不是ThunkDispatch。
我花了很多时间试图找出问题所在,并设法通过创建一个应用程序thunk分派类型找到了解决方法,例如
export type AppThunkDispatch = ThunkDispatch<ApplicationState, void, AnyAction>
在getServerSideProps (或getInitialProps)中使用它时,只需将store.dispatch转换为它即可。
const thunkDispatch = store.dispatch as AppThunkDispatch
我还没有在getServerSideProps上尝试过,但我假设传递给它的商店将与传递给getInitialProps的商店相同……
https://stackoverflow.com/questions/62332738
复制相似问题