首页 文章

StoreEnhancer的TypeScript定义可防止其他参数

提问于
浏览
0

我正在使用React-Redux设置应用程序并配置商店,类似于显示的指南here

这是我的代码:

import { applyMiddleware, compose, createStore } from 'redux';
import thunkMiddleware from 'redux-thunk';
import rootReducer from '../reducers/reducers'

export default function configureStore() {

  const middlewares = [thunkMiddleware];
  const middlewareEnhancer = applyMiddleware(...middlewares);

  const enhancers = [middlewareEnhancer];

  const composedEnhancers = compose(...enhancers);

  const preloadedState = (<any>window).__PRELOADED_STATE__;

  delete (<any>window).__PRELOADED_STATE__;

  const store = createStore(rootReducer, preloadedState, composedEnhancers);

  return store;

}

但是,当我运行build时,我一直收到以下的Typescript错误

TS2345: Argument of type '(...args: any[]) => {}' is not assignable to parameter of type 'StoreEnhancer<{}, {}>'.

我糊涂了 . Redux的声明文件状态(如下所示)不是StoreEnhancer只是将Store和State扩展作为空的普通对象接收吗?

export type StoreEnhancer<Ext = {}, StateExt = {}> = (next: StoreEnhancerStoreCreator) => StoreEnhancerStoreCreator<Ext, StateExt>

如果是这样,为什么不接受其余参数中的'Any'类型,即使我在配置文件中将“noImplicitAny”属性设置为“true”,如下所示?

(据我所知,休息参数无论如何都不能接收声明的类型 . )

我错过了什么?

另外,我使用以下软件包版本:

“react”:“^ 16.4.2”,“redux”:“^ 4.0.0”,“redux-thunk”:“^ 2.3.0”,“webpack”:“^ 4.16.5”,“awesome- typescript-loader“:”^ 5.2.0“,”typescript“:”^ 3.0.3“”@ types / react“:”^ 16.4.12“,”@ types / redux“:”^ 3.6.0“, “@ types / redux-thunk”:“^ 2.1.0”

使用以下TS配置设置:

“compilerOptions”:{“outDir”:“./ dist /”,“sourceMap”:true,“noImplicitAny”:true,“module”:“esnext”,“target”:“esnext”, “jsx”:“react”,“moduleResolution”:“node”,“noUnusedLocals”:true,“noUnusedParameters”:true,“strict”:true,“esModuleInterop”:false,“noFallthroughCasesInSwitch” :true,*“allowSyntheticDefaultImports”:true}

1 回答

  • 0

    问题是 composedEnhancers 的返回类型是 {} ,而 createStore 期望它是 StoreEnhancerStoreCreator<{}, {}> . 返回类型 {} 来自您对 compose 的调用,它在type declarations中匹配此重载,因为您正在传播一组增强器:

    export function compose<R>(...funcs: Function[]): (...args: any[]) => R;
    

    如果未指定 R ,则默认为 {} . 所以要么指定 R ,要么只使用 compose(middlewareEnhancer) ,除非你真的需要一个动态的增强器数组 .

相关问题