网站搜索

typescript - 如何在 nestJS 中移除 Gigwage 客户端的 eslint 错误?


当我在使用 Gigwage 客户端处理服务时,我从 eslint 收到以下错误:

TS2742: The inferred type of 'findAll' cannot be named without a reference to '@gigwage/client/node_modules/axios'. This is likely not portable. A type annotation is necessary
async findAll(findContractorsDto: FindContractorsDto) {
  try {
    return this.gigwageClient.get<{ contractors: Contractor[] }>(
      '/contractors?per_page=10&page=1',
    );
  } catch (e) {
    Logger.error(e);
    throw new Error(e);
  }
}

基本上,有没有办法以某种方式去掉那个 eslint 错误?

答案#1

你必须给函数一个明确的类型。在这种情况下,它将是包含 AxiosResponse 的 Promise。

import { AxiosResponse } from "axios";

async function findAll(findContractorsDto: FindContractorsDto): Promise<
  AxiosResponse<{ contractors: Contractor[]; }, any>
> {
  /* ... */
}

你可能需要从 axios 导入那个类型。