Skip to content

Property 'Authorization' does not exists on type 'AxiosHeaders' after upgrade to 1.2.2 #5416

Closed

Description

@andreylh

Summary

I'm getting the following error after upgrading axios to version 1.2.2:

TS2339: Property 'Authorization' does not exist on type 'AxiosHeaders | Partial<RawAxiosHeaders & MethodsHeaders & CommonHeaders>'.
Property 'Authorization' does not exist on type 'AxiosHeaders'

The same snippet of code is working on version 1.2.1:

import axios from 'axios';

axios.interceptors.request.use((request) => {
    request.headers
        ? (request.headers.Authorization = `Bearer ${token}`)
        : (request.headers = { Authorization: `Bearer ${token}` });
});

Environment

  • Axios 1.2.2
  • Node.js 18.12.1
  • OS: Ubuntu 20.04.4 LTS (WSL2)

Activity

  1. karlhorky commented on Dec 30, 2022

    @karlhorky
    Contributor
  2. karlhorky commented on Dec 30, 2022

    @karlhorky
    Contributor
  3. changed the title [-]Property 'Authorization' does not exist on type 'AxiosHeaders' after upgrade to 1.2.2[/-] [+]Property 'Authorization' does not exists on type 'AxiosHeaders' after upgrade to 1.2.2[/+] on Dec 31, 2022
  4. AmosSzust commented on Dec 31, 2022

    @AmosSzust

    Having the same issue so following...

  5. kirill-linnik commented on Dec 31, 2022

    @kirill-linnik

    same here, rolling back version update so far

  6. tada5hi commented on Dec 31, 2022

    @tada5hi

    @jasonsaayman would be great if you could create a hot fix release ( semver patch ) with #5420.

  7. xgenem commented on Jan 2, 2023

    @xgenem

    Here's how I fixed it

    config.headers = { ...config.headers } as AxiosHeaders;
    config.headers.set('Authorization', token);
    
  8. cmario92 commented on Jan 2, 2023

    @cmario92

    I locked my version to 1.2.1 for now, until this is resolved

  9. xgenem commented on Jan 2, 2023

    @xgenem

    I locked my version to 1.2.1 for now, until this is resolved

    I was in 1.2.1 when it happened. Thought it was fixed in 1.2.2 so I upgraded. Still same issue.

  10. cmario92 commented on Jan 2, 2023

    @cmario92

    I locked my version to 1.2.1 for now, until this is resolved

    I was in 1.2.1 when it happened. Thought it was fixed in 1.2.2 so I upgraded. Still same issue.

    just checked again, on 1.2.1 I don't get this error. only on 1.2.2

  11. xgenem commented on Jan 2, 2023

    @xgenem

    I locked my version to 1.2.1 for now, until this is resolved

    I was in 1.2.1 when it happened. Thought it was fixed in 1.2.2 so I upgraded. Still same issue.

    just checked again, on 1.2.1 I don't get this error. only on 1.2.2

    Correct. The following can still be done in 1.2.1

    config.headers = {
      ...config.headers,
      Authentication: token,
    }
    // Or
    if (config.headers) {
      config.headers.Authorization = token
    } else {
      config.headers = {
        Authorization: token,
      }
    }
    
  12. crypt096 commented on Jan 3, 2023

    @crypt096

    Try something like this if you are using Axios with TS -> (config.headers as AxiosHeaders).set('Authorization', token); it worked in my case.

  13. ruiaraujo012 commented on Jan 3, 2023

    @ruiaraujo012

    Try something like this if you are using Axios with TS -> (config.headers as AxiosHeaders).set('Authorization', token); it worked in my case.

    Thanks, this works for me

  14. DercilioFontes commented on Jan 3, 2023

    @DercilioFontes

    I could solve it using the following:

    const headers = { ...config.headers } as Partial<AxiosRequestHeaders>;
    headers['appversion'] = `${environment.versionNumber}.${environment.buildNumber}`;
    headers['timezone_name'] = dayjs.tz.guess();
    headers['content-location'] = dayjs.tz.guess();
    headers['Authorization'] = `Bearer ${token}`;
    ...
  15. sbahman commented on Jan 4, 2023

    @sbahman

    I am also having this issue - could anyone clarify if this is an intended behaviour after 1.2.2 or is this a "bug" which will likely be fixed in a future release?

  16. 20 remaining items

  17. karlhorky commented on Jan 7, 2023

    @karlhorky
    Contributor

    Seems like another new release of 1.2.3 is happening here:

  18. coo-kiee commented on Jan 11, 2023

    @coo-kiee

    내가 고친 방법은 다음과 같습니다.

    config.headers = { ...config.headers } as AxiosHeaders;
    config.headers.set('Authorization', token);
    

    It's not help to me

    I sovled it:

    axiosInstance.interceptors.request.use(
        (config) => {
            const tempHeader= config.headers as any;
            tempHeader.Authorization = localStorage.getItem(TOKEN_KEY);
            config.headers = tempHeader;
    
            return config;
        },
    );
    
  19. kkirsche commented on Jan 11, 2023

    @kkirsche
    Contributor

    Since this is a bug, rather than working around it with casting I chose to use @ts-expect-error so that typescript will complain when it is fixed.

    Since the code still functionally worked even though the type didn't say it would, I found this to be the least invasive change while still allowing the upgrade.

    Wanted to share in case it helped anyone, thanks for all the time and effort the maintainers and contributors put into this library and its community. Have a great week all 🙂

  20. ffxsam commented on Jan 11, 2023

    @ffxsam

    Wow, today I learned about @ts-expect-error. Cool!

  21. marsDes commented on Jan 12, 2023

    @marsDes
         if (typeof config.headers?.set === 'function') {
            config.headers.set('Authorization', `Bearer ${token}`);
          }
  22. AmosSzust commented on Jan 13, 2023

    @AmosSzust

    Am I missing something or 1.2.3 was not released eventually?
    Is there a problem with it?
    Thanks

  23. xgenem commented on Jan 13, 2023

    @xgenem

    I think it was reverted? Not sure. But for now this should help you

    v1.2.2

    api.interceptors.request.use((config) => {
        const token = '1231231'
        config.headers = { ...config.headers } as AxiosHeaders
        config.headers.set('Authorization', token) 
        return config;
    });
    
  24. AmosSzust commented on Jan 13, 2023

    @AmosSzust

    I saw the many fixes in this thread but I was wondering if there's another issue that prevents this fix from being formally released.

  25. added a commit that references this issue on Jan 14, 2023
  26. curtizdev commented on Jan 15, 2023

    @curtizdev

    I tried this and it works for me, you can also try it.

    axiosInstance.interceptors.request.use(
    async (config) => {
    const token = await getItem("jwtToken");
    if (config.headers)
    (config.headers as AxiosHeaders).set("Authorization", Bearer ${token});
    return config;
    },
    (error) => Promise.reject(error),
    );

  27. DercilioFontes commented on Jan 17, 2023

    @DercilioFontes

    V1.2.3 was released and fixed it.

  28. karlhorky commented on Jan 18, 2023

    @karlhorky
    Contributor

    @andreylh can you confirm that [email protected] fixes the issue? And then close this if it's working now?

  29. tada5hi commented on Jan 18, 2023

    @tada5hi

    Dont't upgrade rash to v1.2.3, it might cause problems :/
    #5476

  30. jasonsaayman commented on Aug 6, 2025

    @jasonsaayman
    Member

    closing as im certain this is fixed

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions