Jes*_*sus 18 typescript react-redux
我有store.js文件
import { createStore, combineReducers } from "redux";
import reducerADD from "../reducer/reducerADD"
export const store = createStore(combineReducers({ reducerADD}));
Run Code Online (Sandbox Code Playgroud)
当我更改格式时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[]'.
Run Code Online (Sandbox Code Playgroud)
这是什么原因?
And*_*ndy 14
状态需要更强的类型。它试图将您的初始状态从 type 转换any为 type never。
在为数据创建默认状态时,您应该为您的状态定义一个接口,这是一个待办事项列表数组的示例:
interface IAppState {
toDoList: any[];
}
const initialState: IAppState = {
toDoList: []
};
export default function reducer(state = initialState, action) {}
Run Code Online (Sandbox Code Playgroud)
对于未来的谷歌员工:
如果您忘记使用与函数定义中使用的相同的输入调用函数,这也是返回的错误。
例子:
const handleSaveAll = (currRow: MyItem, fileToUpload: File | undefined) => {
//all the stuff the function does
});
Run Code Online (Sandbox Code Playgroud)
错误:
<Button className={style.btnSave} onClick={handleSaveAll}>
Run Code Online (Sandbox Code Playgroud)
修复:
<Button className={style.btnSave} onClick={() => handleSaveAll(currRow, fileToUpload)}>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
49553 次 |
| 最近记录: |