PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 13, 2022

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

 October 13, 2022     axios, reactjs, typescript     No comments   

Issue

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


Solution

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)


Answered By - Фарид Ахмедов
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing