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

4
  • Try e.response.status Commented Jul 27, 2022 at 8:04
  • its another status. e.response?.status just error code. But i need to get special status, response from server. Commented Jul 27, 2022 at 8:07
  • The message do you want? what is the special status? for the message use e.response.data.exceptionMessage Commented Jul 27, 2022 at 8:08
  • link this is response from server Commented Jul 27, 2022 at 8:59

1 Answer 1

5

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.