Jest / eslint中的函数缺少返回类型

时间:2020-06-22 13:03:15

标签: javascript node.js typescript async-await jestjs

我的项目中有这个笑话

const action = async () => {
                await hotelService.getByIdAsync(Identifier);
};

await expect(action()).rejects.toThrowError(FunctionalError);

但是我有此错误提示

 92:28  error  Missing return type on function  @typescript-eslint/explicit-function-return-type

2 个答案:

答案 0 :(得分:1)

action函数未指定返回类型,因此会出现错误。为了消除掉毛错误,您需要将返回类型设置为Promise<void>,因为该函数是异步的,因此只返回没有实际值的已解析承诺:

const action = async () : Promise<void> => {
    await hotelService.getByIdAsync(Identifier);
};

答案 1 :(得分:1)

您需要显式指定函数的返回类型。由于函数是async,它返回由Promise返回的数据hotelService.getByIdAsync(Identifier)

const action = async (): Promise</*type of data wrapped in promise*/> => {
   return await hotelService.getByIdAsync(Identifier);
};