我正在尝试使用react + redux + typescript来设置前端环境,但我很难通过combineReducers来使用它。 我收到一个错误:类型的参数不能分配给'ReducersMapObject'类型的参数。请参阅代码下方的完整错误消息。
状态:(types / index.tsx)
export namespace StoreState {
export type Enthusiasm = {
languageName: string;
enthusiasmLevel: number;
}
export type All = {
enthusiasm: Enthusiasm
}
}
STORE :( 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;
REDUCER :( /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;
答案 0 :(得分:2)
您传递的对象包含所有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';