【发布时间】:2021-09-14 12:17:07
【问题描述】:
我正在尝试使用提供的文档将 redux 项目转换为 typescript:
https://redux.js.org/usage/usage-with-typescript#type-checking-middleware
但是,我在使用自定义中间件时遇到了麻烦。这是导致我出错的最小化和提取代码。
store.ts:
import { configureStore } from '@reduxjs/toolkit';
import reducer from './customReducer';
import { customMiddleware } from "./customMiddleware";
const store = configureStore({
reducer: {
custom: customReducer
},
middleware: getDefaultMiddleware => getDefaultMiddleware().prepend(customMiddleware)
})
export type RootState = ReturnType<typeof store.getState>
export default store
customMiddleware.ts:
import { Middleware } from 'redux';
import { RootState } from './store';
export const customMiddleware = (): Middleware<{}, RootState> => {
return store => next => action => {
return next(action);
}
}
这会导致几条错误消息:
在const store = configur...:
'store' 隐式具有类型'any',因为它没有类型注释并且在其自己的初始化程序中直接或间接引用。
关于 RootState 导出:
类型别名 'RootState' 循环引用自身。
关于自定义中间件导出:
'customMiddleware' 隐式具有类型'any',因为它没有类型注释,并且在其自己的初始化程序中直接或间接引用。
【问题讨论】:
标签: typescript redux redux-middleware