axios icon indicating copy to clipboard operation
axios copied to clipboard

Exported classes in type definition are not actually exported

Open wai-ti8m opened this issue 1 year ago • 6 comments

Describe the bug

AxiosHeaders, AxiosError, CanceledError and Axios are "exported" in the type definitions index.d.ts, but not exported in the module.

To Reproduce

const { AxiosHeaders } = require('axios');  // Allowed by Typescript

const headers = new AxiosHeaders({ 'name': 'value' }); // <-- throws Error as AxiosHeaders is not actually exported

Expected behavior

~~Types are not exported, but only declared (declare class instead of export class).~~

Classes are exported and can be imported and used like so:

import { AxiosError, AxiosHeaders, Axios, CanceledError } from 'axios';

new AxiosError();
new AxiosHeaders();
new Axios();
new CanceledError();

Environment

  • Axios Version 1.1.0
  • Node.js Version 16.15.1
  • OS: OSX 12.5
  • Typescript 4.6.3 (any version)
  • React 17

Additional context/Screenshots

none

wai-ti8m avatar Oct 06 '22 10:10 wai-ti8m

I guess that's a duplicate of #5004

wai-ti8m avatar Oct 07 '22 07:10 wai-ti8m

Issue not fixed: Classes are still not exported in the index.js file. Need to add something like export * from './lib/axios.js';

wai-ti8m avatar Oct 07 '22 07:10 wai-ti8m

+1 I am getting this error with 1.1.2

export 'AxiosError' (imported as 'AxiosError') was not found in 'axios' (possible exports: default)

Carsten-Leue avatar Oct 07 '22 13:10 Carsten-Leue

Having the same issue with CanceledError:

import { CanceledError } from 'axios';

console.log(CanceledError); // undefined

And error instanceof CanceledError throws an error because undefined is not an object ¯\_(ツ)_/¯

abriginets avatar Oct 08 '22 02:10 abriginets

Others like AxiosProgressEvent seem to be fine

As a workaround for now I used axios.AxiosError, so for example:

if (error instanceof axios.AxiosError) {

got the idea from this ticket: https://github.com/axios/axios/issues/5062

chrisweb avatar Oct 08 '22 22:10 chrisweb

Another workaround is the use of 'axios.isAxiosError'

Carsten-Leue avatar Oct 09 '22 10:10 Carsten-Leue