我最近将我的react本机项目传递给Typescript,但出现此错误,我找不到解决方法。
错误是:
Types of parameters 'state' and 'state' are incompatible
这是认证还原器:
const initialState = {
credentials: {
Username: '',
Password: '',
},
LoggedIn: false,
};
function authentication(state = initialState, action: any) {
switch (action.type) {
case userConstants.LOGIN_REQUEST:
return {
credentials: action.credentials,
};
case userConstants.LOGIN_SUCCESS:
return { state };
default:
return state;
}
}
export default authentication;
预先感谢您的帮助!
答案 0 :(得分:1)
问题是关于 ActionReducer
的签名。
export interface ActionReducer<T, V extends Action = Action> {
(state: T | undefined, action: V): T;
}
如您所见,它是 T
或 undefined
。但是,在您的减速器中,状态可能仅定义为 T
。这会导致类型问题。