简体   繁体   English

Jest / eslint 中的 function 缺少返回类型

[英]Missing return type on function in Jest / eslint

I have this Jest in my project我的项目中有这个笑话

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

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

but I have this eslint error但我有这个 eslint 错误

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

The action function does not specify a return type, hence the error. action function 未指定返回类型,因此出现错误。 In order to get rid of the linting error, you need to set the return type to Promise<void> as the function is async and thus just returns a resolved promise without an actual value:为了消除 linting 错误,您需要将返回类型设置为Promise<void>因为 function 是异步的,因此只返回已解析的 promise 没有实际值:

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

You need to explicitly specify the return type of the function.您需要明确指定 function 的返回类型。 As function is async , it returns a Promise wrapped around the data returned by hotelService.getByIdAsync(Identifier)由于 function 是async ,它返回一个Promise包裹住hotelService.getByIdAsync(Identifier)返回的数据

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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