【发布时间】:2021-06-20 08:14:53
【问题描述】:
在我的 Redux 异步 thunk 中,我想使用如下所述推断的 AppDispatch 类型:https://redux.js.org/recipes/usage-with-typescript
我按照这里的说明进行操作:https://redux.js.org/recipes/usage-with-typescript#typing-createasyncthunk
但是当在我的自定义 ThunkApiConfig 类型(以下示例中为MyThunkApiConfig)中使用此 AppDispatch 类型时,将出现循环引用,如下所示:https://codesandbox.io/s/focused-joliot-ho45h?file=/src/usersSlice.ts:
'dispatch' is referenced directly or indirectly in its own type annotation.
这是有道理的,因为调度类型和使用的 thunk 之间存在循环依赖关系。但是如何在异步 thunk 中使用 AppDispatch?
提前感谢您的回答!
【问题讨论】:
-
这绝对是一个不寻常的设置。使
createAsyncSlice将异步重击作为参数而不是直接使用变量的目标是什么?我可以肯定地找出类型,但我不明白这一点。 -
类型是
dispatch是DispatchForMiddlewares<M> & Dispatch<A>其中M是中间件,A是操作类型。我需要查找 thunk 中间件的类型。
标签: typescript redux redux-thunk redux-toolkit