【发布时间】:2021-08-04 11:41:27
【问题描述】:
我正在使用 redux-toolkit,并尝试发送一个 thunk。 我正在使用的调度函数看起来像这样,这是直接取自文档。 eslint 抱怨缺少返回类型,知道它应该是什么吗?
export const useAppDispatch = () => useDispatch<AppDispatch>();
此外,我得到了同样的错误,就像这个问题How to dispatch an Action or a ThunkAction (in TypeScript, with redux-thunk)?一样,但据我了解,那里没有真正的解决方案。
如果我得到这样的代码:
export const fetchSomeThing = createAsyncThunk<SomeType[]>('someThing/fetch', async () => {
const someClient = useSomeClient();
const someThing: SomeType[] = await someClient.listSomething();
return someThing;
});
// I also tried typing dispatch as AppDispatch here explicitly, but it gives me the same error
const dispatch = useAppDispatch();
dispatch(fetchSomeThing()) //This gives an error: Property 'type' is missing in type 'AsyncThunkAction<SomeType[], void, {}>'
// but required in type 'AnyAction'.ts(2345), index.d.ts(21, 3): 'type' is declared here.
我的商店是这样的:
export const store = configureStore({
reducer: {
someThing: someThingReducer,
},
});
// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch; // According to vs-code this is of type AppDispatch = Dispatch<AnyAction>
【问题讨论】:
标签: reactjs typescript redux redux-toolkit