【问题标题】:Updated Typescript - Receiving Error on Combine Reducers Function TS2344更新的打字稿 - 在组合减速器功能 TS2344 上接收错误
【发布时间】: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


【解决方案1】:

npm i typescript@3.8.3后问题解决

【讨论】:

    【解决方案2】:

    另一个完整的解决方案在这里:

    function combineReducers(reducers: R)

    来源:https://github.com/ionic-team/ionic-react-conference-app/issues/50

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    猜你喜欢
    • 2021-12-21
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 2020-04-20
    • 1970-01-01
    • 2018-08-23
    相关资源
    最近更新 更多