初始状态有错误:类型为 '{ email: string; 的参数密码:字符串;有效:布尔值; }' 不可分配给“never”类型的参数。ts(2345)
function reducer(state: IState, action: IFluxAction) {
const Users = UsersFetch()
switch (action.type) {
case 'email':
const email = action.value
const valid = Users.find(e => e === email) ? true : false
return {
email,
valid,
}
case 'password':
const password = action.value
return {
password,
}
default:
throw new Error()
}
}
const initialState = {
email: '',
password: '',
valid: false,
}
const [state, dispatch] = React.useReducer(reducer, initialState)
输入此内容以满足错误的正确方法是什么?
反应16.8.1
打字稿 3.3.1
应该是(被固定)
adding ...state
到回报,就像
switch (action.type) {
case 'email':
const email = action.value
const valid = Users.find(e => e === email) ? true : false
return {
...state,
email,
valid,
}
case 'password':
const password = action.value
return {
...state,
password,
}
default:
throw new Error()
}
另外 - 正如 @madflanderz 所建议的,将 IState 设置为减速器的预期返回值有助于捕获此类问题。