Open evelynhathaway opened 1 month ago
@remcohaszing I saw you were an author on axios
's ESM types and you fixed #371. You seem knowledgeable about this area, and I hope you don't mind the ping. ❤️
I think https://github.com/axios/axios/pull/6218 would resolve the issue.
Any chance there is some sort of workaround to make this work until this is merged in?
Overview
With
axios@1.7.7
,axios-mock-adapter@2.1.0
,typescript@5.6.3
, 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 isaxios@1.5.1
, the type change seems to be introduced inaxios@1.6.0
. 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
axios@1.5.1
, 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: https://github.com/softonic/axios-retry/issues/159#issuecomment-1809406232. 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