인프런 커뮤니티 질문&답변
axios catch 에서 error 타입에 대해 as 없이 이렇게 사용하면 어떨까요?
작성
·
358
0
interface IResponse<T = {}> {
data: T;
message: string;
}
interface IUser {
id: string;
email: string;
name: string;
}
class CustomError<T> extends AxiosError<T> {
constructor(...args: any[]) {
super(...args);
}
static isCustomError<T>(value: any): value is CustomError<T> {
return value instanceof CustomError<T>;
}
}
axios.get('').then().catch((error: unknown) => {
if (CustomError.isCustomError<IResponse<IUser>>(error)) {
// console.error(
// (error as AxiosError<{ message: string }>.response?.data.message)
// )
const errorMessage = error.response?.data.message;
}
})response의 대해서 타입이 좁혀지긴 하지만, 지저분해서 맞는 방법인지 모르겠네요






axios.isAxiosError 사용해도 타입 좁혀지네요!
감사합니다 ㅎㅎ