没有重载匹配此调用。过载1,共2

时间:2019-10-04 09:50:06

标签: typescript react-redux

我有store.js个文件

import { createStore, combineReducers } from "redux";
import reducerADD from "../reducer/reducerADD"

export const store = createStore(combineReducers({ reducerADD}));

当我在store.tsx中更改格式时,出现错误:

No overload matches this call.
  Overload 1 of 2, '(reducers: ReducersMapObject<{ reducerADD: { lastName: string; firstName: string; password: string; email: string; }[]; }, any>): Reducer<{ reducerADD: { lastName: string; firstName: string; password: string; email: string; }[]; }, AnyAction>', gave the following error.
    Type '(state: never[] | undefined, action: action) => { lastName: string; firstName: string; password: string; email: string; }[]' is not assignable to type 'Reducer<{ lastName: string; firstName: string; password: string; email: string; }[], any>'.
      Types of parameters 'state' and 'state' are incompatible.
        Type '{ lastName: string; firstName: string; password: string; email: string; }[] | undefined' is not assignable to type 'never[] | undefined'.
          Type '{ lastName: string; firstName: string; password: string; email: string; }[]' is not assignable to type 'never[]'.
            Type '{ lastName: string; firstName: string; password: string; email: string; }' is not assignable to type 'never'.
  Overload 2 of 2, '(reducers: ReducersMapObject<{ reducerADD: { lastName: string; firstName: string; password: string; email: string; }[]; }, AnyAction>): Reducer<{ reducerADD: { lastName: string; firstName: string; password: string; email: string; }[]; }, AnyAction>', gave the following error.
    Type '(state: never[] | undefined, action: action) => { lastName: string; firstName: string; password: string; email: string; }[]' is not assignable to type 'Reducer<{ lastName: string; firstName: string; password: string; email: string; }[], AnyAction>'.
      Types of parameters 'state' and 'state' are incompatible.
        Type '{ lastName: string; firstName: string; password: string; email: string; }[] | undefined' is not assignable to type 'never[] | undefined'.
          Type '{ lastName: string; firstName: string; password: string; email: string; }[]' is not assignable to type 'never[]'.

这是什么原因?

2 个答案:

答案 0 :(得分:6)

需要更强地键入状态。它试图将您的初始状态从类型any转换为类型never

在为数据创建默认状态时,您应该为状态定义一个接口,这是一个待办事项列表数组的示例:

interface IAppState {
  toDoList: any[];
}
const initialState: IAppState = {
  toDoList: []
};

export default function reducer(state = initialState, action) {}

答案 1 :(得分:0)

针对未来的Google员工:

如果您忘记使用与函数定义中相同的输入来调用函数,这也是返回的错误。

示例:

const handleSaveAll = (currRow: MyItem, fileToUpload: File | undefined) => {
    //all the stuff the function does
});

错误:

<Button className={style.btnSave} onClick={handleSaveAll}>

解决方法:

<Button className={style.btnSave} onClick={() => handleSaveAll(currRow, fileToUpload)}>