![](/img/trans.png)
[英]Typescript : ESLint : Unsafe return of an any typed value @typescript-eslint/no-unsafe-return
[英]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()
})
}
我一直收到此行的 lint 问题。then((response): object => response.data)
说明“任何”类型表达式的不安全使用
我怀疑这是因为response
是“通用对象”,而 typescript 无法“识别”它具有.data
属性。
为了解决这个问题,我们可以声明一个类型的接口:
type hasData = { data: any };
然后用它向 TS “解释” 我们期望响应包含该属性:
.then((response: hasData): object => response.data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.