我有一个休息呼叫,它可以发送回一个我想存储的标题。因此,我需要在分配它之前检查它是否在那里。
到目前为止,我做了这样的事情:
private getHeader(response: AxiosResponse) {
if (response.headers.has('myheader')) {
storeHeader(response.headers['myheader']);
}
}
但是Typescript抱怨response.headers.has
。它说:
TS18049: 'response.headers.has' is possibly 'null' or 'undefined'.
TS2723: Cannot invoke an object which is possibly 'null' or 'undefined'.
TS2349: This expression is not callable. Not all constituents of type 'string | number | boolean | AxiosHeaders | string[] | ((header: string, matcher?: true | AxiosHeaderMatcher | undefined) => boolean)' are callable. Type 'string' has no call signatures.
1条答案
按热度按时间tjrkku2a1#
AxiosResponse#headers的类型为
RawAxiosResponseHeaders | AxiosResponseHeaders
,AxiosResponseHeaders的类型为RawAxiosResponseHeaders & AxiosHeaders
。方法
has
在类AxiosHeaders
中定义,因此我们需要在使用headers instanceof AxiosHeaders
调用has
之前将response.headers
缩小到AxiosHeaders
。另一个答案的代码更少:-)