-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript error when axios instance is created from an import that uses axios >=1.6.0
's ESM types
#400
Comments
@remcohaszing I saw you were an author on |
I think axios/axios#6218 would resolve the issue. |
Any chance there is some sort of workaround to make this work until this is merged in? |
@yourinium as @evelynhathaway said a dirty "any" cast will keep it compiling until we have a proper fix, e.g. // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
axiosMock = new MockAdapter(axios as any); |
It's worth noting that the aforementioned axios/axios#6218 was merged, but reverted in 1.7.9 due to making a breaking change that broke Typescript compilation upstream for a lot of users (axios/axios#6720). So it'll be longer still before we see this fixed upstream. |
Overview
With
[email protected]
,[email protected]
,[email protected]
, usingMockAdapter
causes TypeScript to error about type differences betweenaxios
when imported via our ESM code and the types imported byaxios-mock-adapter
.The newest
axios
version that this does not occur is[email protected]
, the type change seems to be introduced in[email protected]
. However, I don't believe it's anaxios
issue, or at least one that's as straightforward as you may expect.axios
's types ESM and CJS are composed of the same literals and unions forAxiosRequestHeaders
, so I feel like TypeScript is providing an unhelpful error about assignability of members of what is functionally the same union. The crux is that the classesAxiosHeaders
(ESM) andaxios.AxiosHeaders
(CJS) are not assignable.Besides downgrading to
[email protected]
, this also doesn't occur when using.cts
or"type": "script"
. Unfortunately downgrading to a vulnerableaxios
version and rewriting our modules to CommonJS are not feasible workarounds for my team, so we'reas any
'ing the errors away.Possible Fix
It appears that
axios-retry
fixed this assignability issue occurring from the different type files by using ESM or CJSaxios
types based on whetheraxios-retry
was imported from ESM or CJS: softonic/axios-retry#159 (comment). Perhaps this approach would work foraxios-mock-adapter
?CodeSandbox
https://codesandbox.io/p/devbox/yxnj2p?file=%2Fsrc%2Findex.ts%3A6%2C1
Error
Minimal TypeScript Code Triggering Error
TypeScript Config
The text was updated successfully, but these errors were encountered: