Skip to content

Broken AxiosHeaders typings #5415

Closed

Description

@nfantone

Describe the bug

AxiosHeaders types makes it so trying to call methods on the instance raises a typescript compilation error.

image

AxiosRequestConfig.headers can be either RawAxiosRequestHeaders or AxiosHeaders. RawAxiosRequestHeaders, being an alias of Record<string, AxiosHeaderValue> causes methods to not be callable.

To Reproduce

Invoke any method on AxiosRequestConfig.headers.

Code snippet

const api = axios.create({ ... })

api.interceptors.request.use(req => {
  // Trying to access any method on `req.headers` will result in
  // a "This expression is not callable" TS error
  req.headers.set('foo', 'bar')
})

Expected behavior

Should be able to call any method declared in AxiosHeaders.

Environment

  • axios: 1.2.2
  • node: 18.12.1
  • typescript: 4.9.4
  • macOS Ventura 13.1

Activity

  1. nfantone commented on Dec 29, 2022

    @nfantone
    Author

    In addition to the above, since AxiosRequestConfig.headers is an optional property, it's not straightforward to set headers using regular object access notation — i.e.: config.headers['foo'].

    image

    Is the expectation to coerce the type of .headers before accessing it?

  2. karlhorky commented on Dec 30, 2022

    @karlhorky
    Contributor

    Seems like this is also similar to #5416, which may also be fixed by #5420

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