在 Typescript 函数中对 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)

哪个州 不安全地使用 'any'

类型的表达式

我怀疑这是因为 response 是一个“通用对象”,打字稿无法“识别”它具有 .data 属性。

为了解决这个问题,我们可以声明一个类型的接口:

type hasData = { data: any };

然后用它向 TS “解释”我们希望响应包含该属性:

.then((response: hasData): object => response.data)