axios: Invalid `paramsSerializer` TS type
Describe the bug
The TS interface for paramsSerializer
key in the AxiosRequestConfig
is wrong / incomplete.
To Reproduce
Try to add a custom paramsSerializer
function
config.paramsSerializer = (params) => qs.stringify(params, { arrayFormat: 'repeat' })
Expected behavior
I can add a custom paramsSerializer
function without getting type errors
Environment
- Axios Version:
1.1.0
- TypeScript Version:
4.7.4
Additional context/Screenshots
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 18
@RNR1 Axios already supports this out of the box. By default, Axios encodes arrays in “bracket” format but supports 3 qs formats except “comma”.
So to encode in
arrayFormat: 'repeat'
you need to do the following:Echo response:
Undocumented API change. Not cool.
@pegiadise Just had same issue now, I found the
encode
option inparamsSerializer
.To fix the error you need to do next:
Shouldn’t it be:
?
@DigitalBrainJS
is really helpful, it should be added to the official documentation
how do i fix this in my case? @babazookz @alete89 @dduft
error message: Type ‘(params: any) => string’ has no properties in common with type ‘ParamsSerializerOptions’
Yes, the
paramsSerializer
option now has a totally different interface/implementation. Theencode
option is primarily for custom data escaping. The custom key/value pair encoder is not currently supported for query params (only supported for FormData pairs), although it is defined as avisitor
method in the TS interface, it is currently ignored. Its support will be added soon. Basically, Axios has its own encoder that handles common cases of encoding nested data structures, so most users won’t encounter the need for custom parameter conversions.