【发布时间】:2021-09-03 08:20:18
【问题描述】:
我仍在努力理解 TypeScript,但我不确定这在这里试图告诉我什么。任何帮助将不胜感激。
此代码正在接收减速器对象的对象。在编译我的 React/Ionic 项目时,我现在在更新底层库后收到以下错误:
.../src/data/combineReducers.ts(3,47) 中的 TypeScript 错误:
- 类型“R[K]”不满足约束“(...args: any) => any”。
- 类型 'R[keyof R]' 不能分配给类型 '(...args: any) => any'。
- 键入'R[字符串] | R[号码] | R[symbol]' 不可分配给类型 '(...args: any) => any'。
- 类型 'R[string]' 不能分配给类型 '(...args: any) => any'。 TS2344
[react-scripts] 1 | export function combineReducers<R extends any>(reducers: R) {
[react-scripts] 2 | type keys = keyof typeof reducers;
[react-scripts] > 3 | type returnType = { [K in keys]: ReturnType<typeof reducers[K]> }
[react-scripts] | ^
[react-scripts] 4 | const combinedReducer = (state: any, action: any) => {
[react-scripts] 5 | const newState: returnType = {} as any;
[react-scripts] 6 | const keys = Object.keys(reducers);
export function combineReducers<R extends any>(reducers: R) {
type keys = keyof typeof reducers;
type returnType = { [K in keys]: ReturnType<typeof reducers[K]> }
const combinedReducer = (state: any, action: any) => {
const newState: returnType = {} as any;
const keys = Object.keys(reducers);
keys.forEach(key => {
const result = reducers[key](state[key], action);
newState[key as keys] = result || state[key];
});
return newState;
}
return combinedReducer;
};
【问题讨论】:
-
你的代码在哪里?
标签: reactjs typescript ionic-framework reducers