[英]Unsafe use of expression of type 'any' for return statements in Typescript function
async fetchDetail(token: string): Promise < object > {
const headersRequest = {
Authorization: `Basic ${token}`,
'Content-Type': 'application/json',
}
return await this.httpService.get( < URL > , {
headers: headersRequest
})
.toPromise()
.then((response): object => response.data)
.catch(() => {
throw new NotFoundException()
})
}
I keep getting a lint issue for this line .then((response): object => response.data)我一直收到此行的 lint 问题。then((response): object => response.data)
which states Unsafe use of expression of type 'any'说明“任何”类型表达式的不安全使用
I suspect that it's because response
is a "generic object" and typescript can't "identify" that it has a .data
attribute.我怀疑这是因为
response
是“通用对象”,而 typescript 无法“识别”它具有.data
属性。
In order to fix that we can declare an interface of a type:为了解决这个问题,我们可以声明一个类型的接口:
type hasData = { data: any };
and then use it to "explain" to TS that we expect the response to contain that attribute:然后用它向 TS “解释” 我们期望响应包含该属性:
.then((response: hasData): object => response.data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.