Skip to content
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

Argument of type 'AxiosInstance' is not assignable to parameter of type 'AxiosStatic | AxiosInstance' #159

Closed
nwaughachukwuma opened this issue Dec 10, 2020 · 17 comments · Fixed by #250 or #253

Comments

@nwaughachukwuma
Copy link

When I try to configure axiosRetry using an axios client as specified in the usage section of the README

// Works with custom axios instances
const client = axios.create({ baseURL: 'http://example.com' });
axiosRetry(client, { retries: 3 });

I get the following error:

Argument of type 'AxiosInstance' is not assignable to parameter of type 'AxiosStatic | AxiosInstance'

Setup Info:
Platform: macOS Mojave v10.14.6
Typescript: v4.1.2
Node.js: v12.18.3

@hugebdu
Copy link

hugebdu commented Feb 7, 2021

same here.

Platform: macOS Cataline v10.15.7
Typescript: v3.9.7
Node.js: v12.19.0

@kmeshavkin
Copy link

I had that problem too - in my case it was old version of axios that caused this error (I had v0.18.1).

I also have yarn workspaces set up and axios was listed in one of workspace's package.json as v0.21.1 (last version currently), but main node_modules still resolved to v0.18.1 (probably due to some other dependency), so I added this to main package.json and now error is fixed:

"resolutions": {
  "axios": "0.21.1"
}

@wjureczka
Copy link

Hi there!
I have the same problem:
Node.js: 14.15.2
TypeScript: 4.2.4
Platform: macOS

package.json:
"axios": "0.19.2", "axios-hooks": "2.7.0", "axios-retry": "3.2.0",
image

@xisvaldo
Copy link

xisvaldo commented Mar 3, 2022

Hello, same error here, any updates?

Node.JS 16.13.1
TypeScript 4.6.2
Axios 0.24.0
Axios Retry 3.2.4
Linux

@shaunkh
Copy link

shaunkh commented Apr 16, 2022

Was this ever solved? @rubennorte

@C-o-d-e-C-o-w-b-o-y
Copy link

+1

1 similar comment
@yatishhr
Copy link

+1

@matsgm
Copy link

matsgm commented May 30, 2023

I had same error with:

"axios": "^1.4.0",
"axios-retry": "^3.4.0"

Added:

"resolutions": {
    "axios": "1.4.0"
}

which fixed it.

@obernardovieira
Copy link

same here, but resolutions didn't fix. Although the issue is just on Heroku. Locally there are no problems. I've checked the versions.

@viktornord
Copy link

This is still an issue, please fix it

@yutak23
Copy link
Contributor

yutak23 commented Jul 4, 2023

Sorry if I am wrong. I hope this is of some help to you.

It may be due to a different version of axios.
I think this may happen when multiple versions of axios are under node_modules due to dependencies.

#159 (comment)

Since resolutions is an option in yarn that allows you to fix the version of a package deep in the dependency tree, it seems to me that the error is caused by a type mismatch due to a different version, if resolutions solves the problem.

import axiosRetry, { exponentialDelay } from 'axios-retry'; // <- version of axios on which the type definition depends is 1.2.3
import axios from 'axios'; // <- this axios is 1.4.0

axiosRetry(axios, { retryDelay: exponentialDelay }); // compile error

My full error logs is below.

$ npx tsc
index.ts:4:12 - error TS2345: Argument of type 'AxiosStatic' is not assignable to parameter of type 'AxiosInstance | AxiosStatic'.
  Type 'AxiosStatic' is not assignable to type 'AxiosInstance'.
    Types of property 'defaults' are incompatible.
      Type 'Omit<import("/home/study/workspace/axios-retry/spec/typings/cjs/node_modules/axios/index").AxiosDefaults<any>, "headers"> & { headers: import("/home/study/workspace/axios-retry/spec/typings/cjs/node_modules/axios/index").HeadersDefaults & { ...; }; }' is not assignable to type 'Omit<import("/home/study/workspace/axios-retry/node_modules/axios/index").AxiosDefaults<any>, "headers"> & { headers: import("/home/study/workspace/axios-retry/node_modules/axios/index").HeadersDefaults & { ...; }; }'.
        Type 'Omit<AxiosDefaults<any>, "headers"> & { headers: HeadersDefaults & { [key: string]: AxiosHeaderValue; }; }' is not assignable to type 'Omit<AxiosDefaults<any>, "headers">'.
          Types of property 'transformRequest' are incompatible.
            Type 'import("/home/study/workspace/axios-retry/spec/typings/cjs/node_modules/axios/index").AxiosRequestTransformer | import("/home/study/workspace/axios-retry/spec/typings/cjs/node_modules/axios/index").AxiosRequestTransformer[]' is not assignable to type 'import("/home/study/workspace/axios-retry/node_modules/axios/index").AxiosRequestTransformer | import("/home/study/workspace/axios-retry/node_modules/axios/index").AxiosRequestTransformer[]'.
              Type 'AxiosRequestTransformer' is not assignable to type 'AxiosRequestTransformer | AxiosRequestTransformer[]'.
                Type 'import("/home/study/workspace/axios-retry/spec/typings/cjs/node_modules/axios/index").AxiosRequestTransformer' is not assignable to type 'import("/home/study/workspace/axios-retry/node_modules/axios/index").AxiosRequestTransformer'.
                  The 'this' types of each signature are incompatible.
                    Type 'AxiosRequestConfig<any>' is not assignable to type 'InternalAxiosRequestConfig<any>'.
                      Types of property 'headers' are incompatible.
                        Type 'import("/home/study/workspace/axios-retry/node_modules/axios/index").AxiosRequestHeaders' is not assignable to type 'import("/home/study/workspace/axios-retry/spec/typings/cjs/node_modules/axios/index").AxiosRequestHeaders'.
                          Type 'AxiosRequestHeaders' is not assignable to type 'Partial<RawAxiosHeaders & { Accept: AxiosHeaderValue; "Content-Length": AxiosHeaderValue; "User-Agent": AxiosHeaderValue; "Content-Encoding": AxiosHeaderValue; Authorization: AxiosHeaderValue; } & { ...; }>'.
                            'string' index signatures are incompatible.
                              Type 'AxiosHeaderValue' is not assignable to type 'import("/home/study/workspace/axios-retry/spec/typings/cjs/node_modules/axios/index").AxiosHeaderValue'.
                                Type 'AxiosHeaders' is not assignable to type 'AxiosHeaderValue'.
                                  Type 'AxiosHeaders' is missing the following properties from type 'AxiosHeaders': setAuthorization, getAuthorization, hasAuthorization, [Symbol.iterator]

4 axiosRetry(axios, { retryDelay: exponentialDelay });
             ~~~~~

Found 1 error in index.ts:4

@razor-x
Copy link

razor-x commented Nov 2, 2023

Started getting this after updating from Axios 1.5 to 1.6. Probably related to https://github.com/axios/axios/pull/5931/files

@yutak23
Copy link
Contributor

yutak23 commented Nov 14, 2023

I have looked into the details of this compile error.

Problem

Since we are using one index.d.ts in axios-retry, it is the same index.d.ts for both ES Module and CommonJS. In other words, the type definition of axios imported by import * as axios from 'axios'; will be index.d.cts.

Specifically, module "/home/study/workspace/ts-node-oidc/node_modules/axios/index" means that axios-retry is a CommonJS package, so node_modules/ axios/index.d.cts, since axios-retry is a CommonJS package.

This will be the same whether the project using axios-retry is configured for ES Module or CommonJS.
image

And the trouble is that axios index.d.cts (type definitions for CommonJS) and index.d.ts (type definitions for ES Module) are incompatible.

// package.json
	"type": "module",
import axios from 'axios'; // <- if package.json type is "module", imported axios is `node_modules/axios/index.d.ts`
import axiosRetry from 'axios-retry';

axiosRetry(axios, { retries: 3 });

Therefore, with the above implementation, the type definition on the axios side will be node_modules/axios/index.d.ts, and the axiso imported in axios-retry will be node_modules/axios/index.d.cts. So, this causes the following compile error.

$ npx tsc
srv/app.ts:7:12 - error TS2345: Argument of type 'AxiosStatic' is not assignable to parameter of type 'AxiosStatic | AxiosInstance'.
  Type 'import("/home/study/workspace/ts-node-oidc/node_modules/axios/index", { assert: { "resolution-mode": "import" } }).AxiosStatic' is not assignable to type 'import("/home/study/workspace/ts-node-oidc/node_modules/axios/index").AxiosStatic'.
    Types of property 'create' are incompatible.
      Type '(config?: import("/home/study/workspace/ts-node-oidc/node_modules/axios/index", { assert: { "resolution-mode": "import" } }).CreateAxiosDefaults<any> | undefined) => import("/home/study/workspace/ts-node-oidc/node_modules/axios/index", { assert: { "resolution-mode": "import" } }).AxiosInstance' is not assignable to type '(config?: import("/home/study/workspace/ts-node-oidc/node_modules/axios/index").CreateAxiosDefaults<any> | undefined) => import("/home/study/workspace/ts-node-oidc/node_modules/axios/index").AxiosInstance'.
        Types of parameters 'config' and 'config' are incompatible.
          Type 'import("/home/study/workspace/ts-node-oidc/node_modules/axios/index").CreateAxiosDefaults<any> | undefined' is not assignable to type 'import("/home/study/workspace/ts-node-oidc/node_modules/axios/index", { assert: { "resolution-mode": "import" } }).CreateAxiosDefaults<any> | undefined'.
            Type 'import("/home/study/workspace/ts-node-oidc/node_modules/axios/index").CreateAxiosDefaults<any>' is not assignable to type 'import("/home/study/workspace/ts-node-oidc/node_modules/axios/index", { assert: { "resolution-mode": "import" } }).CreateAxiosDefaults<any>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
              Types of property 'headers' are incompatible.
                Type 'AxiosHeaders | Partial<import("/home/study/workspace/ts-node-oidc/node_modules/axios/index").HeadersDefaults> | Partial<RawAxiosHeaders & { ...; } & { ...; }>' is not assignable to type 'import("/home/study/workspace/ts-node-oidc/node_modules/axios/index", { assert: { "resolution-mode": "import" } }).AxiosHeaders | Partial<import("/home/study/workspace/ts-node-oidc/node_modules/axios/index", { assert: { "resolution-mode": "import" } }).HeadersDefaults> | Partial<...>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
                  Type 'Partial<HeadersDefaults>' is not assignable to type 'AxiosHeaders | Partial<HeadersDefaults> | Partial<RawAxiosHeaders & { Accept: AxiosHeaderValue; "Content-Length": AxiosHeaderValue; "User-Agent": AxiosHeaderValue; "Content-Encoding": AxiosHeaderValue; Authorization: AxiosHeaderValue; } & { ...; }>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
                    Type 'Partial<HeadersDefaults>' is not assignable to type 'Partial<RawAxiosHeaders & { Accept: AxiosHeaderValue; "Content-Length": AxiosHeaderValue; "User-Agent": AxiosHeaderValue; "Content-Encoding": AxiosHeaderValue; Authorization: AxiosHeaderValue; } & { ...; }>'.
                      Property 'common' is incompatible with index signature.
                        Type 'Partial<RawAxiosHeaders & { Accept: AxiosHeaderValue; "Content-Length": AxiosHeaderValue; "User-Agent": AxiosHeaderValue; "Content-Encoding": AxiosHeaderValue; Authorization: AxiosHeaderValue; } & { ...; }>' is not assignable to type 'AxiosHeaderValue | undefined'.
                          Type 'Partial<RawAxiosHeaders & { Accept: AxiosHeaderValue; "Content-Length": AxiosHeaderValue; "User-Agent": AxiosHeaderValue; "Content-Encoding": AxiosHeaderValue; Authorization: AxiosHeaderValue; } & { ...; }>' is missing the following properties from type 'string[]': length, pop, push, concat, and 29 more.

7 axiosRetry(axios, { retries: 3 });
             ~~~~~


Found 1 error in srv/app.ts:7

Solution

It would be quite difficult to modify index.d.ts and index.d.cts in axios to be compatible, so I think it would be better to change the export of type definitions in axios-retry, depending on whether it is ES Module or CommonJS.

@yuvall-cyera
Copy link

Hello
Do you know when you'll release a new version with this fix?
Snyk currently warns about older axios versions and an upgrade to 1.6.0 should be done
https://security.snyk.io/vuln/SNYK-JS-AXIOS-6032459

@mindhells
Copy link
Member

Hello Do you know when you'll release a new version with this fix? Snyk currently warns about older axios versions and an upgrade to 1.6.0 should be done https://security.snyk.io/vuln/SNYK-JS-AXIOS-6032459

the fix for this issue is already available in 3.9.0 thanks to @yutak23

@mindhells
Copy link
Member

Reopened because the #250 has been reverted in 3.9.1

The plan is now to migrate to TS implementation

@mindhells
Copy link
Member

This should be fixed in v4.0.0

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