我正在尝试使用提供的文档将 redux 项目转换为打字稿:
https://redux.js.org/usage/usage-with-typescript#type-checking-middleware
但是,我在使用自定义中间件时遇到了麻烦。这是对我来说导致错误的最小化和提取代码。
商店.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);
}
}
这会导致几个错误消息: on const store = configur...
:
'store' 隐式具有类型'any',因为它没有类型注释并且在其自己的初始化程序中直接或间接引用。
关于 RootState 导出:
类型别名 'RootState' 循环引用自身。
关于 customMiddleware 导出:
'customMiddleware' 隐式具有类型'any',因为它没有类型注释并且在其自己的初始化程序中直接或间接引用。