You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
AxiosHeaders types makes it so trying to call methods on the instance raises a typescript compilation error.
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
constapi=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 errorreq.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
Reacted by Mario Menéndez, Marvin ROGER, Andras Lassu, pohuing, Thibault Vlacich, 구동현, kingchen-bct, avlapeto, Peter, James Gennoy and 12 more
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'].
Is the expectation to coerce the type of .headers before accessing it?
Activity
nfantone commentedon Dec 29, 2022
In addition to the above, since
AxiosRequestConfig.headersis an optional property, it's not straightforward to set headers using regular object access notation — i.e.:config.headers['foo'].Is the expectation to coerce the type of
.headersbefore accessing it?karlhorky commentedon Dec 30, 2022
Seems like this is also similar to #5416, which may also be fixed by #5420