![](/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.