繁体   English   中英

对 Typescript function 中的 return 语句不安全地使用“any”类型的表达式

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM