Skip to content

AxiosError should be a top-level export instead of a static #5062

Closed

Description

@GuichiZhao

Describe the bug

There is no way to use AxiosError correctly (It should be used as both type and value)

To Reproduce

import { AxiosError as AxiosErrorFromImport } from 'axios';
import axios from '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
 */
const errorHandler = (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'?
const errorHandler1 = (error: AxiosError) => {};

// Event though I follow the error prompts
const errorHandler2 = (error: typeof AxiosError) => {
  // 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

Environment

  • Axios 1.1.2
  • Typescript 4.6.3

Activity

  1. brillaintlcd commented on Oct 8, 2022

    @brillaintlcd

    I am opening a PR on this 👍 !

  2. chrisweb commented on Oct 8, 2022

    @chrisweb

    This might be related to #5031

  3. GuichiZhao commented on Oct 9, 2022

    @GuichiZhao
    Author

    I am opening a PR on this +1 !

    The problem is not name conflicting, it is the wrong way (not typescript wise) of exporting

  4. brillaintlcd commented on Oct 9, 2022

    @brillaintlcd

    Okay so I should change the way it is exported, right?

  5. GuichiZhao commented on Oct 9, 2022

    @GuichiZhao
    Author

    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

  6. brillaintlcd commented on Oct 9, 2022

    @brillaintlcd

    I have fixed it 👍🏻 !

    Can you take a look at it?

  7. GuichiZhao commented on Oct 10, 2022

    @GuichiZhao
    Author

    I have fixed it 👍🏻 !

    Can you take a look at it?

    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.

  8. jasonsaayman commented on Oct 10, 2022

    @jasonsaayman
    Member

    Will have a look @haneenmahd

  9. littledian commented on Oct 12, 2022

    @littledian
    Contributor

    It seems pr: #5030 fixed it but then commit: #2149464bb4f12c1101b15f73298a060e92470376 revert it

  10. Erazihel commented on Oct 20, 2022

    @Erazihel

    I tried to update axios to v1.1.3 but the error is still here unfortunately.

    const axiosError = new AxiosError();
    TypeError: _axios.AxiosError is not a constructor
    
  11. littledian commented on Oct 21, 2022

    @littledian
    Contributor

    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

  12. emmaLP commented on Mar 19, 2025

    @emmaLP

    @GuichiZhao have you been able to solve the issue at all?

    Hitting this issue with v1.8.2 when I try instantiate AxiosError for a axios mock.

    Still getting this error: TypeError: _axios.AxiosError is not a constructor

  13. self-assigned this
    on Jul 30, 2025
  14. added
    issue::enhancement
    on Mar 29, 2026
  15. jasonsaayman commented on Jun 17, 2026

    @jasonsaayman
    Member

    Closing AxiosError is now a top-level runtime export

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

issue::enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions