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
We have a common use-case of chaining certain request headers from inbound HTTP requests (in this case using express) along on outbound requests. Prior to axiosv0.21.4 (specifically #3021), we didn't see type incompatibility pulling headers off of an express request and putting them directly into the outbound AxiosRequestConfigheaders. With the new type, we now see
error TS2322: Type 'string | string[]' is not assignable to type 'string'
To Reproduce
import{AxiosRequestConfig}from'axios';import{Request}from'express';functionaddHeadersFromRequest(config: AxiosRequestConfig,request: Request): AxiosRequestConfig{config.headers['testheader']=request.headers['testheader'];// error TS2322: Type 'string | string[]' is not assignable to type 'string'}
Expected behavior
The types here used to work, though I am in favor of more strict types that actually represent what axios expects and supports. This doesn't seem like an uncommon use-case. I see two options here:
axios supports string[] header values, since these are a supported possibility in Node.js
axios does not support string[] header values, meaning library consumers need to adapt themselves
The error here is because the expressheaders type comes from @types/nodeIncomingMessage, which has headers: IncomingHttpHeaders. For any headers not explicitly defined in IncomingHttpHeaders, the type is [header: string]: string | string[] | undefined;. This now mismatches with axiosstring value expectation.
Somewhat related to #4193 (type change causing issues) and #4141. It would be great if axios types better aligned with the raw node types, as this would provide a more consistent experience across the ecosystem, but I understand the desire not to make breaking changes (maybe part of v1 as others have suggested?).
The text was updated successfully, but these errors were encountered:
To build on this, as mentioned for the other types in #4144, I don't think it would hurt to add string[] to the allowed types anyway, since string[] can automatically be concatenated to a string type as well. That would also help deal with the issue mentioned in #4136
Describe the bug
We have a common use-case of chaining certain request headers from inbound HTTP requests (in this case using
express
) along on outbound requests. Prior toaxios
v0.21.4
(specifically #3021), we didn't see type incompatibility pulling headers off of anexpress
request and putting them directly into the outboundAxiosRequestConfig
headers
. With the new type, we now seeTo Reproduce
Expected behavior
The types here used to work, though I am in favor of more strict types that actually represent what
axios
expects and supports. This doesn't seem like an uncommon use-case. I see two options here:axios
supportsstring[]
header values, since these are a supported possibility in Node.jsaxios
does not supportstring[]
header values, meaning library consumers need to adapt themselvesEnvironment
"0.24.0"
12.20.1
MacOS 11.6.1
Additional context/Screenshots
The error here is because the
express
headers
type comes from@types/node
IncomingMessage
, which hasheaders: IncomingHttpHeaders
. For any headers not explicitly defined inIncomingHttpHeaders
, the type is[header: string]: string | string[] | undefined;
. This now mismatches withaxios
string
value expectation.Somewhat related to #4193 (type change causing issues) and #4141. It would be great if
axios
types better aligned with the rawnode
types, as this would provide a more consistent experience across the ecosystem, but I understand the desire not to make breaking changes (maybe part of v1 as others have suggested?).The text was updated successfully, but these errors were encountered: