actionLog提示错误,得到直接或间接引用,请问是什么原因?
来源:9-7 【中间件】使用redux-thunk中间价实现异步action

慕仔5555278
2021-02-03
store.ts 里的export type RootState = ReturnType<typeof store.getState>
RootState报错,提示Type alias ‘RootState’ circularly references itself.ts(2456)
actionLog.ts里的export const actionLog: Middleware<{}, RootState> = (store) => (next) => (action) => {}
actionLog报错,提示’actionLog’ is referenced directly or indirectly in its own type annotation.ts(2502)
1回答
-
这个问题非常好。是的,会发生这样的情况是因为rootState导出的是store的类型,而middelware又属于store本身的一部分,所以会抱这个错误。
我们可以进行以下的修改
export type RootState = ReturnType<typeof store.getState>
改为
export type RootState = ReturnType<typeof rootReducer>
这样就解决问题了。
稍微解释一下为什么可以改为“ReturnType<typeof rootReducer>”。因为rootReducer实际上就是一系列函数的返回对象拼在一起,而这个对象的则定义了redux store中state的类型,所以我们也可以使用ReturnType<typeof rootReducer>。
或许我们更应该使用“ReturnType<typeof rootReducer>”来减少redux对象之间的依赖性。我会在课程更新结束以后对这个部分做一个更新。
非常感谢你的问题。
022021-02-07
相似问题