You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
AxiosError should be a top-level export instead of a static #5062
There is no way to use AxiosError correctly (It should be used as both type and value)
To Reproduce
import{AxiosErrorasAxiosErrorFromImport}from'axios';importaxiosfrom'axios';/** * AxiosError is resolved as a Class from the "import" * Class can be used as both type and value in typescript * Such structure should be the right way to do types * The following code has no compile time error * * BUT it has a runtime error ERROR because the Implementation * is not export, which is a disaster */consterrorHandler=(error: AxiosErrorFromImport)=>{console.log(AxiosErrorFromImport.ERR_BAD_OPTION);console.log(error.response);};/** * Actually, the implementation is added to the default export as static */const{ AxiosError }=axios;/** * By doing this AxiosError is not treated as a class ANYMORE * so the following code has a compile time error */// 'AxiosError' refers to a value, but is being used as a type here// Did you mean 'typeof AxiosError'?consterrorHandler1=(error: AxiosError)=>{};// Event though I follow the error promptsconsterrorHandler2=(error: typeofAxiosError)=>{// Property 'config' does not exist on type 'typeof AxiosError'.error.config};// There is no way to use the type declaration// So, what is the point to put AxiosError as a static?// It totally screwed up the type declaration:// AxiosError can be used as a type by importing from top-level// AxiosError can be used as a value by geting it from default import// It is very confusiong and inconvenient
Expected behavior
Put AxiosError as top-level export with implementation rather than type ONLY
Okay so I should change the way it is exported, right?
In my opinion, the whole class (both type and implementation) should be exported at top level. Currently only type (interface) is exported at top level, the real implementation is hidden deep inside, which is the root of the trouble
I tried it after upgrade axios to v1.1.3, It works when I import it but not work when require it;
eg:
It works: import axios from 'axios' or import { AxiosError } from 'axios';
It not works in broswer: const axios = require('axios'), but it works in nodejs env
Activity
brillaintlcd commentedon Oct 8, 2022
I am opening a PR on this 👍 !
chrisweb commentedon Oct 8, 2022
This might be related to #5031
GuichiZhao commentedon Oct 9, 2022
The problem is not name conflicting, it is the wrong way (not typescript wise) of exporting
brillaintlcd commentedon Oct 9, 2022
Okay so I should change the way it is exported, right?
GuichiZhao commentedon Oct 9, 2022
In my opinion, the whole class (both type and implementation) should be exported at top level. Currently only type (interface) is exported at top level, the real implementation is hidden deep inside, which is the root of the trouble
fix: export AxiosError class at top-level
brillaintlcd commentedon Oct 9, 2022
I have fixed it 👍🏻 !
Can you take a look at it?
GuichiZhao commentedon Oct 10, 2022
I am not maintainer of axios and do not want to involve too much, you can explain the reason and ask someone relevant to do the reviewing.
jasonsaayman commentedon Oct 10, 2022
Will have a look @haneenmahd
littledian commentedon Oct 12, 2022
It seems pr: #5030 fixed it but then commit: #2149464bb4f12c1101b15f73298a060e92470376 revert it
Erazihel commentedon Oct 20, 2022
I tried to update
axiostov1.1.3but the error is still here unfortunately.littledian commentedon Oct 21, 2022
I tried it after upgrade axios to v1.1.3, It works when I import it but not work when require it;
eg:
It works: import axios from 'axios' or import { AxiosError } from 'axios';
It not works in broswer: const axios = require('axios'), but it works in nodejs env
emmaLP commentedon Mar 19, 2025
@GuichiZhao have you been able to solve the issue at all?
Hitting this issue with
v1.8.2when I try instantiate AxiosError for a axios mock.Still getting this error:
TypeError: _axios.AxiosError is not a constructorjasonsaayman commentedon Jun 17, 2026
Closing AxiosError is now a top-level runtime export