admin管理员组文章数量:1432178
I'm trying to send request via Axios and I want to typify request headers. But I get an error.
I've created an interface Headers and used it to declare variable this._apiHeaders
.
Error text:
The "Headers" type cannot be assigned to the "AxiosRequestHeaders" type.
There is no index signature for the "string" type in the "Headers" type (ts2322)
interface Headers {
'X-Parse-Application-Id': string,
'X-Parse-REST-API-Key': string
}
const response: ITest = await axios.get(
this.formCorrectApiUrl(Endpoints.classes, DBObjectName),
{
headers: this._apiHeaders // Here I get error
}
);
Can someone please tell me how to fix the error and why it occures.
I'm trying to send request via Axios and I want to typify request headers. But I get an error.
I've created an interface Headers and used it to declare variable this._apiHeaders
.
Error text:
The "Headers" type cannot be assigned to the "AxiosRequestHeaders" type.
There is no index signature for the "string" type in the "Headers" type (ts2322)
interface Headers {
'X-Parse-Application-Id': string,
'X-Parse-REST-API-Key': string
}
const response: ITest = await axios.get(
this.formCorrectApiUrl(Endpoints.classes, DBObjectName),
{
headers: this._apiHeaders // Here I get error
}
);
Can someone please tell me how to fix the error and why it occures.
Share Improve this question asked Jun 20, 2022 at 16:25 WellsWells 1312 silver badges11 bronze badges2 Answers
Reset to default 5This can be implemented through "extends"
interface Headers extends AxiosRequestHeaders {
'X-Parse-Application-Id': string
'X-Parse-REST-API-Key': string
}
But I remend making additionally the ability to create fields optional.
export type NonUndefined<T, E = undefined> = Pick<
T,
{
[Prop in keyof T]: T[Prop] extends E ? never : Prop
}[keyof T]
>
interface Headers extends AxiosRequestHeaders {
'X-Parse-Application-Id': string
'X-Parse-REST-API-Key': string
}
type AxiosHeaders = NonUndefined<Headers>
const headers: AxiosHeaders = {
'X-Parse-Application-Id': 'test',
}
Please try this
import axios, { AxiosRequestHeaders } from 'axios';
await axios.get(url, { headers: (this._apiHeaders as any as AxiosRequestHeaders) })
本文标签: javascriptHow to typify axios request headersStack Overflow
版权声明:本文标题:javascript - How to typify axios request headers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745579185a2664518.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论