Yegor Dovydenko
Yegor Dovydenko

Reputation: 51

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

(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?

Upvotes: 4

Views: 3911

Answers (1)

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)

Upvotes: 5

Related Questions