-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Strict await #35419
Comments
It's perfectly valid in JavaScript to await on a non-promise expression:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await If you really want to prevent this, then a linter seems much more appropriate. |
I know, that's why I explained my reasining in the use case section. I think this is similar to checking the thruthyness of a function value - that's also perfectly valid, but most likely a bug. I'm glad TypeScript now reports this an an error. |
|
@kzok Thanks, didn't know this existed. Indeed, a linter seems to be appropiate for this. |
This is intentional; |
@RyanCavanaugh, could this strict mode simply not error on the former case where a type is possibly a Promise (eg., As for the latter case, could it not be handled by either leaving the mode disabled, or using |
Using This seems to be a job for a linter, but not for the compiler. |
Counterpoint: Couldn't the same be said for uncalled function checks? To lay bare my personal motivations, we currently use the |
@RyanCavanaugh is there documentation with guidance on where this line is drawn – what kinds of issues the compiler would be willing to treat as warnings vs. these lighter "advisories"? It seems like there are some examples where code that's technically valid can be treated as a compilation warning or error (e.g. uncalled / always-truthy methods in |
That sounds like some low-level weirdness that shouldn't be encouraged, and opted-in somehow. |
Another letdown by TS. Consider this code:
This code should not compile. But at least there's an eslint rule. |
Search Terms
errors on await on non-promise values, await expecting then()
Suggestion
I'd like an option (e.g.
strictAwait
) that causesawait
on non-promise expressions to be an error.Awaiting values of union types where one option is a promise probably should still be ok, so you can await on something that could be a promise or a value.
The following conditional type would reflect this behavior AFAIK:
await
would then only be allowed on values of typeT
whereMayBePromise<T>
is equal totrue
.Use Cases
While it's not a direct runtime error to
await
a non-promise value, it's still most likely a bug, because you expected something to be a promise, but it isn't.The most common case for me is a missing call to
toPromise()
on an observable I just want to activate without caring about the result. This is an example using apollo-client, where themutate
method returns a cold observable that you need to subscribe to in order to actually do something:If you omit the
toPromise()
, everything still typechecks, but nothing will happen.Calling
await
on immediate values won't do any harm (apart from performance probably), but it's still misleading, and it will be a hard-to-find bug when athen
method will be added to the value in the future.Examples
This should still compile:
This should not compile:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: