我将此函数转换为switch语句,但现在在我的测试套件中出现此错误。
Type boolean is not assignable to HttpError
这是if else
语句:
handleSubmitError = (
error: HttpError,
{ setSubmitting, setErrors, setStatus }: FormikActions<ITypeOfValues>
) => {
const genericError = 'An error occurred. Please try again';
setSubmitting(false);
if (!error.response || (error.response && !error.response.data)) {
setStatus(genericError);
} else if (
error.response &&
error.response.status === 400 &&
Array.isArray(error.response.data) &&
error.response.data.length > 0
) {
setErrors(mapServerValidationErrorsToFormik(error.response.data));
} else {
setStatus(error.response.data.toString() ? error.response.data.toString() : genericError);
}
};
所以,这是我转换为switch语句:
handleSubmitError = (
error: LensesHttpError,
{ setSubmitting, setErrors, setStatus }: FormikActions<ITypeOfValues>
) => {
const genericError = 'An error occurred. Please try again';
setSubmitting(false);
switch (error) {
case error === !error.response || (error.response && !error.response.data):
return setStatus(genericError);
case error === error.response &&
error.response.status === HTTP_STATUS_CODE.BAD_REQUEST &&
Array.isArray(error.response.data) &&
error.response.data.length > 0:
return setErrors(mapServerValidationErrorsToFormik(error.response.data));
default:
return setStatus(
error.response.data.toString() ? error.response.data.toString() : genericError
);
}
};
这是HttpError的类型:export interface HttpError extends AxiosError {}
这是针对AxiosError:
export interface AxiosError extends Error {
config: AxiosRequestConfig;
code?: string;
request?: any;
response?: AxiosResponse;
}
我做错了什么,为什么我的测试失败了。我的意思是我要传递给错误的布尔值是什么?谢谢!