axios: Types are still broken with 1.2.4 for interceptors

Describe the bug

Assigning custom interceptors that accept and return AxiosRequestConfig works with 1.2.2, but breaks with 1.2.4.

This is used by e.g. https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/axios-token-interceptor, and also in user code.

Code snippet

axios.interceptors.request.use((config: AxiosRequestConfig) => Promise.resolve(config));

Error message

error TS2345: Argument of type '(config: AxiosRequestConfig) => Promise<AxiosRequestConfig<any>>' is not assignable to parameter of type '(value: InternalAxiosRequestConfig<any>) => InternalAxiosRequestConfig<any> | Promise<InternalAxiosRequestConfig<any>>'.
  Type 'Promise<AxiosRequestConfig<any>>' is not assignable to type 'InternalAxiosRequestConfig<any> | Promise<InternalAxiosRequestConfig<any>>'.

150         axios.interceptors.request.use((config: AxiosRequestConfig) => Promise.resolve(config));
                                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


### Expected behavior

It should not break, this is a minor upgrade.

### Axios Version

1.2.4

### Adapter Version

_No response_

### Browser

_No response_

### Browser Version

_No response_

### Node.js Version

18.12.1

### OS

_No response_

### Additional Library Versions

```bash
Typescript

Additional context/Screenshots

No response

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 24
  • Comments: 22

Commits related to this issue

Most upvoted comments

Config inside interceptors has a different interface. If you explicitly specify the config type, use InternalAxiosRequestConfig instead of AxiosRequestConfig. AxiosRequestConfig is the external/raw config interface.

axios.interceptors.request.use(async (config: InternalAxiosRequestConfig) => {

});

const config: AxiosRequestConfig = {};

axios(config);

Try this

export function authorizationInterceptor(internalAxiosRequestConfig: InternalAxiosRequestConfig) {
    internalAxiosRequestConfig.headers.set( "your-header-key-here", "yourheader-value-here");
    return internalAxiosRequestConfig;
}

Can confirm after 1.2.2 is breaking change for typescript users.

Can confirm after 1.2.2 is breaking change for typescript users.

Still not working with TS after v1.4.0

you can just downgrade your Axios version = “axios”: “^0.24.0” … it’s working fine

The interceptor signature seems to have changed, fix by using header.set( .. , .. ) works

On Tue, 21 Mar 2023 at 13:36, YR @.***> wrote:

Can confirm after 1.2.2 is breaking change for typescript users.

thank you, use 1.2.1, it works

— Reply to this email directly, view it on GitHub https://github.com/axios/axios/issues/5494#issuecomment-1477682764, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFANXKKJVRO7NHY5U7QMMP3W5GHC5ANCNFSM6AAAAAAUFRTVYU . You are receiving this because you commented.Message ID: @.***>

Kwasi Owusu Gyasi-Agyei @.*** \ +27 (81) 466 - 4488

Until this is fixed, I added as any where needed…

Also not working with Axios v1.5.0 I am afraid.

Still broken version axios version 1.3.4

I already mentioned it in #5476 for 1.2.3 (see here) and the problem still persists in version 1.2.4: the headers of the config in AxiosResponse are not optional anymore (as it was in 1.2.1). In 1.2.4, the config has the type InternalAxiosRequestConfig (in code) which is still a breaking change. It affects then also the interceptors as described in the first post.

Most likely though this package is deprecated as it depends on Axios 0.x.

"axios": "^0.19.2",

The incompatibility is only in the TypeScript types.

Still broken version axios version 1.3.3

@DigitalBrainJS it’s harder than I expected. Pushed my changes to https://github.com/DefinitelyTyped/DefinitelyTyped/pull/64075, but the test fails.

This is the test:

const provider = tokenProvider(validOptions1); // $ExpectType TokenProvider
const oldConfig: AxiosRequestConfig = {
    headers: {}
};
const newConfig: InternalAxiosRequestConfig = {
    headers: new AxiosHeaders()
};
provider(oldConfig); // $ExpectType Promise<AxiosRequestConfig<any>>
provider(newConfig); // $ExpectType Promise<InternalAxiosRequestConfig<any>>

and this is the error:

ERROR: 32:1  expect  TypeScript@4.7 expected type to be:
  Promise<InternalAxiosRequestConfig>
got:
  Promise<AxiosRequestConfig<any>>