![](/img/trans.png)
[英]Typescript error 'argument of type is not assignable to parameter of type'
[英]CombineReducers with Typescript returns error “Argument of type is not assignable to parameter of type 'ReducersMapObject'”
我正在尝试使用react + redux + typescript来设置前端环境,但我很难让它与combineReducers一起使用。 我收到一个错误:类型的参数不能分配给'ReducersMapObject'类型的参数。 请参阅代码下方的完整错误消息。
状态:(types / index.tsx)
export namespace StoreState {
export type Enthusiasm = {
languageName: string;
enthusiasmLevel: number;
}
export type All = {
enthusiasm: Enthusiasm
}
}
商店:(store.tsx)
import { createStore } from 'redux';
import reducers from './reducers/index';
import { StoreState } from './types/index';
let devtools: any = window['devToolsExtension'] ? window['devToolsExtension']() : (f:any)=>f;
const Store = createStore<StoreState.All>(reducers, devtools);
export default Store;
减速器:( /reducers/HelloReducer.tsx)
import { EnthusiasmAction } from '../actions';
import { StoreState } from '../types/index';
import { INCREMENT_ENTHUSIASM, DECREMENT_ENTHUSIASM } from '../constants/index';
export const enthusiasm = (state: StoreState.Enthusiasm,
action: EnthusiasmAction): StoreState.Enthusiasm => {
switch (action.type) {
case INCREMENT_ENTHUSIASM:
return { ...state, enthusiasmLevel: state.enthusiasmLevel + 1 };
case DECREMENT_ENTHUSIASM:
return { ...state, enthusiasmLevel: Math.max(1, state.enthusiasmLevel - 1) };
default:
return state;
}
}
COMBINE REDUCERS(/reducers/index.tsx)
import { StoreState } from '../types/index';
import * as enthusiasmReducer from './HelloReducer';
import { combineReducers } from 'redux';
const reducer = combineReducers<StoreState.All>({
enthusiasm: enthusiasmReducer
});
export default reducer;
你传递的对象是HelloReducer
的所有导出而不仅仅是reducer。 有几种方法可以解决它。 您可以选择减速器:
const reducer = combineReducers<StoreState.All>({
enthusiasm: enthusiasmReducer.enthusiasm
});
或仅导入reducer:
import {enthusiasm} from './HelloReducer';
..
const reducer = combineReducers({enthusiasm});
或者添加export default enthusiasm;
到HelloReducer.tsx
并将导入更改为
import enthusiasmReducer from './HelloReducer';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.