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

Broken AxiosHeaders typings #5415

Closed
nfantone opened this issue Dec 29, 2022 · 2 comments · Fixed by #5420
Closed

Broken AxiosHeaders typings #5415

nfantone opened this issue Dec 29, 2022 · 2 comments · Fixed by #5420

Comments

@nfantone
Copy link

nfantone commented Dec 29, 2022

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
@nfantone
Copy link
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?

@karlhorky
Copy link
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
Labels
None yet
Projects
None yet
3 participants