Home > other >  What type of axios error i need for getting error.response.data.status?
What type of axios error i need for getting error.response.data.status?

Time:07-28

(e: AxiosError) => console.log(e.response?.data.status)

Here i have an error (Object is of type 'unknown'). But server give me response like

{status: 'error description'}

With "any" of course it work perfect. But it's not a solution. How can i use typescript at this situation?

CodePudding user response:

AxiosError has generic parameter to type:

export interface AxiosError<T = any> extends Error {
  config: AxiosRequestConfig;
  code?: string;
  request?: any;
  response?: AxiosResponse<T>;
  isAxiosError: boolean;
  toJSON: () => object;
}

And it's any by default. You just need to specify type of T:

(e: AxiosError<{ status: string }>) => 
     console.log(e.response?.data.status)
  • Related