reactjs - Redux 和 Typescript 中间件
问题描述
我正在尝试将 Javascript 编写的 React 应用程序转换为 Typescript,但它真的让我很难受。错误消息对我来说真的很难理解。我试图创建一个简单的中间件,但它给出了一个错误,我一直在尝试解决大约 5 个小时。
const loggerMiddleware: Middleware = (api: MiddlewareAPI) => (next: Dispatch<AnyAction>) => (action) => {
const returnValue = next(action)
console.log('state after dispatch', api.getState())
// This will likely be the action itself, unless
// a middleware further in chain changed it.
return returnValue
}
export default function configureStore(history: History, initialState?: ApplicationState) {
const middleware = [
loggerMiddleware
];
const rootReducer = combineReducers({
...reducers,
router: connectRouter(history)
});
const enhancers = [];
const windowIfDefined = typeof window === 'undefined' ? null : window as any;
if (windowIfDefined && windowIfDefined.__REDUX_DEVTOOLS_EXTENSION__) {
enhancers.push(windowIfDefined.__REDUX_DEVTOOLS_EXTENSION__());
}
return createStore(
rootReducer,
initialState,
compose(applyMiddleware(...middleware), ...enhancers)
);
}
Severity Code Description Project File Line Suppression State Suppression State
Error TS2345 (TS) Argument of type '{ loggerMiddleware: Middleware<{}, any, Dispatch<AnyAction>>; }' is not assignable to parameter of type 'Middleware<any, any, any>'.
Type '{ loggerMiddleware: Middleware<{}, any, Dispatch<AnyAction>>; }' provides no match for the signature '(api: MiddlewareAPI<any, any>): (next: Dispatch<AnyAction>) => (action: any) => any'.
编译器想表达什么?
解决方案
看起来您loggerMiddleware
正在作为命名导出导出。在代码的底部文件中按名称(将其括在括号中)导入它:
import { loggerMiddleware } from '../where/the/file/is.js';
推荐阅读
- python - Boto3:如何获取从 Spot 请求创建的实例 ID?
- r - 根据 r 中 var 的值生成所有可能的假人
- python - 如何从加密图像中获取二维码?
- android - 选项卡布局可能导致内存泄漏
- c# - 应该兼容的不兼容的 .NET Standard 程序集?
- node.js - 无法在节点 js 中显示静态文件夹中的图像
- mysql - Symfony 4 SQLSTATE [42000] 指定的键太长
- ios - 您如何控制要下载的图像数量?
- ios - 自定义标头在发送到服务器时转换为小写(使用 NSURLSession)
- flutter - 如何实现一次只刷一张卡以消除颤动的动作?