typescript 没有与此调用匹配的重载,重载2中的1,

vulvrdjw  于 2023-01-10  发布在  TypeScript
关注(0)|答案(4)|浏览(555)

我有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[]'.

原因何在?

mznpcxlj

mznpcxlj1#

状态需要稍微强一些。它试图将初始状态从类型any强制转换为类型never
在创建数据的默认状态时,应该为状态定义一个接口,下面是一个todo列表数组的示例:

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

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

fhg3lkii2#

对于未来的谷歌人:
如果您忘记使用函数定义中使用的相同输入调用函数,也会返回此错误。
示例:

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)}>
wfauudbj

wfauudbj3#

我遇到了同样的问题,然后我发现我使用的是console.table对象,因此我将其改为console.log console.log对象.它解决了我的问题

hkmswyz6

hkmswyz64#

在我的例子中,错误来自于一个格式错误的循环

cards.map(card => {
        <EasyGridItem cols={ card.cols }>
            <BandcampDailyCard card={ card } /> 
        </EasyGridItem>
    })

刚刚将{}更换为()

相关问题