-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Bug: promise-function-async
is reporting on () => A | Promise<B>
#10687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
As you mentioned - the rule docs explicitly state this and they also include a workaround if it is your intention to return a non-promise and a promise:
The change requested is a suggestion fixer which must be manually applied by a user. Suggestion fixers (unlike autofixers) are allowed to be unsafe or incomplete as they must be manually actioned. |
But this is not what the error says: "Functions that return promises must be async." The error message, as is, is incomplete/incorrect because the function does not return only a promise. The reason why I opened this issue is specifically because I found an Changes suggested, one or more:
Regarding the point 5: the rule appears to be enforcing three separate issues:
|
In the vast, vast, vast majority of cases the developer intent is to just return a promise and not to have a hybrid return type - and if you asyncify the function then you get that exact behaviour - so the rule reporting is correct and guides the user towards better code. But there are cases where you needs to intentionally NOT asyncify the function because you NEED a hybrid output. Instead of just relying on a disable comment here we chose to take the return type as explicit developer intent that a hybrid return type was desired. So put another way:
So the rule reports in (1) and (3) and not in (2). |
Right, but that's what I meant in my last point, the rule is broad and it enforces something that is not in the explicit intent of the rule. Even if you don't agree to change that, suggestions 1 and 3 still apply (error message + additional fixer)
Yeah, that's why it would be great to update the error message to suggest this. Most developers barely read the error messages, let alone googling the rule name to read its docs. |
Again I want to stress that in the vast, vast, vast majority of cases a user DOES NOT WANT A HYBRID RETURN TYPE and instead the hybrid return type is an inference error caused by a function that returns a plain and a promise value. Eg cases like this: function doSomethingAsync(): Promise<number>;
function doSomething(condition: boolean) {
if (condition) {
return 1;
}
return doSomethingAsync();
} Generally the intent here is "this function returns |
My 2c is that
So removing the behaviour goes against that established and expected practice. I'd be okay with us updating the error message if the function has a hybrid return type to mention that an explicit annotation will silence the rule. [1] iirc this case was added after the rule was first created but regardless it's near enough to "always". |
Before You File a Bug Report Please Confirm You Have Done The Following...
Playground Link
https://typescript-eslint.io/play/#ts=5.7.2&fileType=.ts&code=FAMwrgdgxgLglgewgAgEYAoCUyDexnIBOApjGISgLICGMAFgHSHUQAmCAtlsgPzIDkSYv2QAuZAAVCnOAGdiTYrIQAbAG7F0ARkwBuYAF8gA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6AewFtLlFaAM1hMy%2BSpya0AhskKj0URNC7RI4MAF8QmoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false
Repro Code
ESLint Config
tsconfig
Expected Result
No reports
Actual Result
"Functions that return promises must be async."
Attempts to fix to
async function
, changing the return type fromA | Promise<B>
toPromise<A | B>
Additional Info
While this behavior is described in the docs/demo, I believe it's wrong because the function does not "return a promise," it only "MAY return a promise."
The change requested is unsafe as it changes the signature/type of the function entirely.
Note that whether
() => A | Promise<B>
is a good type is not relevant to this issue. The type exists in the wild (web extensions, screenshot) and it can be prohibited by a separate rule (suggested here)The text was updated successfully, but these errors were encountered: