initialState 有错误:类型为 '{ 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)
键入此内容以满足错误的正确方法是什么?
react 16.8.1 typescript 3.3.1
应该(通过)
添加...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 设置为 reducer 的预期返回值有助于捕捉这样的问题。