arthurfiorette / axios-cache-interceptor

📬 Small and efficient cache interceptor for axios. Etag, Cache-Control, TTL, HTTP headers and more!
https://axios-cache-interceptor.js.org
MIT License
703 stars 58 forks source link

Bug: error TS2345: Argument of type 'AxiosStatic' is not assignable to parameter of type 'AxiosInstance' #746

Closed amrendr-cb closed 10 months ago

amrendr-cb commented 11 months ago

What happened?

I am trying to use in typescript as follow but typescript throws me error TS2345: Argument of type 'AxiosStatic' is not assignable to parameter of type 'AxiosInstance'. This is how documentation describe its usage, am I using incorrectly?

import Axios from 'axios';
import { setupCache } from 'axios-cache-interceptor';

const axios = setupCache(Axios);

Typescript - ^5.3.3

axios-cache-interceptor version

v1.3.2

Node / Browser Version

Node v18.18.0

Axios Version

v1.6.2

What storage is being used

Memory Storage

Relevant debugging log output

Typescript code doesn't compile, so no debug log available.
paescuj commented 11 months ago

It's currently targeting axios@1.4.0, looks like the default export & types have changed in v1.6.

darkmacheken commented 11 months ago

Due to a vulnerability in axios < 1.6.0 [1], this should have a higher priority Currently is impossible to update the package to a version that fixes the vulnerability without breaking code

[1] https://github.com/advisories/GHSA-wf5p-g6vw-rhxx

paescuj commented 11 months ago

FYI: Seems to work with axios@1.6.2, I believe it's just a type issue. Until fixed you can ignore it with the // @ts-ignore directive.

Nevertheless, would still be awesome to have this fixed soon ❤️

arthurfiorette commented 11 months ago

I do not have spare time to work on this currently, does someone want to work on it?

paescuj commented 11 months ago

Happy to look at it later that day 👍

arthurfiorette commented 11 months ago

I got time to take a look at it and got it working normally with axios-cache-interceptor@1.3.2 and axios@1.6.2. I'll close this issue until a minimal reproducible example is provided.

import Axios from 'axios'; // 1.6.2
import { setupCache } from 'axios-cache-interceptor'; // 1.3.2

const axios = Axios.create();

const globalInstance = setupCache(Axios); // works fine!
const instance = setupCache(axios); // also works fine!
amrendr-cb commented 11 months ago

@arthurfiorette, thanks for looking into the issue, but I am still having the issue. For now, I have used @paescuj suggestion to ignore the type error as this appears to be just a typing error but the code still works.

Here is an example https://github.com/amrendr-cb/axios-cache-examples

arthurfiorette commented 11 months ago

Hey, I don't have any clue on the reason for this problem, as its being used the same type with Axios and setupCache. However I could manage to temporarily fix it out and released v1.4.1. Feel free to reopen this issue if the problem persists.

amrendr-cb commented 11 months ago

@arthurfiorette, your previous patch got it working with ts-node but it still fails to compile with tsc

node_modules/axios-cache-interceptor/dist/cache/create.d.ts:23:43 - error TS2709: Cannot use namespace 'Axios' as a type.

23 export declare function setupCache(axios: Axios, options?: CacheOptions): AxiosCacheInstance;
arthurfiorette commented 11 months ago

I have tested it in your repo and some of my own, what's the version of tsc you are running?

paescuj commented 11 months ago

Sorry for my late reply! I've now been able to take a look at it.

The issue is that there are no explicit declaration files for ESM, so TypeScript is falling back to the CJS declarations which isn't compatible with the Axios types when running in a ESM context.

https://arethetypeswrong.github.io/?p=axios-cache-interceptor%401.4.1

amrendr-cb commented 10 months ago

I have tested it in your repo and some of my own, what's the version of tsc you are running?

@arthurfiorette, didn't got a chance to get back to you earlier. I have pushed your fix to my example repo.

Now typescript code work properly - npm start But when building - npx tsc or npm run build it errors out.

I have tsc v5.3.3

Thanks for looking into the issue and Happy new year!