Skip to content

AxiosRequestHeaders type breaks from 0.21.4 to 0.22.0 #4193

@mxsxs2

Description

@mxsxs2

Describe the bug

The type has changed and now I am getting linting errors on a working code.
I get Element implicitly has an 'any' type because index expression is not of type 'number'.ts(7015) error when I try to access the headers by their name on a AxiosRequestHeaders object.

For example accessing a common header in transfomRequest where the content of headers is:
image

If I try to access the common Accept header key, then I will get the above error.

I think the issue is at:

export type AxiosRequestHeaders = Record<string, string>;

The type should be:

export type AxiosRequestHeaders = Record<string, Record<string, string>>; 

Possibly the AxiosResponseHeaders has the same issue.

To Reproduce

import axios from 'axios'

axios({
    method: 'post',
    url: '/user/12345',
    data: {
        firstName: 'Fred',
        lastName: 'Flintstone',
    },
    transformRequest: (_, headers) => {
        if(headers)
            delete headers.common['Accept'] //Error occurs here
    },
})
    .then(function (response) {
        console.log(response)
    })
    .catch(function (error) {
        console.log(error)
    })

Expected behavior

Expected to not raise an error there

Environment

  • Axios 0.23.0
  • TypeScript 4.4.4

Additional context/Screenshots

image

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