You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect lexing of TLA await in try...catch #38483
Today I was adding some code to a function and needed to make the function async. But I accidentally put the async keyword on the wrong function and my code became something like this:
function f() {
await(async () => 0)();
}
And typescript gives me this strange error:
a.ts:2:5 - error TS2304: Cannot find name 'await'.
2 await(async () => 0)();
~~~~~
Found 1 error.
And I'm like WHAT THE??? WHAAAT?? WHY?? And I search for the error message and I find this issue, and I'm very very confused for a long time, thinking that typescript does not support syntax like await (async () => 0)() and I say to myself very loudly "WHY in the world would such a compiler not support such syntax..."
I now realize that I have put the async keyword on the wrong function, and I've now learned (the strange way,) that you actually CAN define a variable named await, but I think that the error message in such a case as above should not be something like Cannot find name 'await'....
Update: And now, after writing this, I'm looking at this issue again, and I realize that typescript actually does not support such syntax in some cases. Seems like I only found a case that the support is there, but a bad error message made me think it was not. And I'm now thinking WHY would fixing this issue be postponed for such a long time...
[...] but I think that the error message in such a case as above should not be something like Cannot find name 'await'....
Outside of an async function or the top level of a module, await is considered an identifier, so the error isn't wrong, per se. We can consider introducing a better error message for this specific case, considering you might have meant to declare the function as async. We already do that if your operand is not parenthesized: function f() { await 1; } gives you an error and a quick fix to "Add async modifier to containing function". We just don't currently offer that quick fix when we initially parsed it as a call expression.
Activity
lissein commentedon May 12, 2020
Hi,
This seems to be a problem with all the top level statements of the form
await (async () => { ... })()and not only in try...catch.I'm gonna work on it.
Shayan-To commentedon Sep 4, 2021
Today I was adding some code to a function and needed to make the function
async. But I accidentally put theasynckeyword on the wrong function and my code became something like this:And typescript gives me this strange error:
And I'm like WHAT THE??? WHAAAT?? WHY?? And I search for the error message and I find this issue, and I'm very very confused for a long time, thinking that typescript does not support syntax like
await (async () => 0)()and I say to myself very loudly "WHY in the world would such a compiler not support such syntax..."I now realize that I have put the
asynckeyword on the wrong function, and I've now learned (the strange way,) that you actually CAN define a variable namedawait, but I think that the error message in such a case as above should not be something likeCannot find name 'await'....Update: And now, after writing this, I'm looking at this issue again, and I realize that typescript actually does not support such syntax in some cases. Seems like I only found a case that the support is there, but a bad error message made me think it was not. And I'm now thinking WHY would fixing this issue be postponed for such a long time...
rbuckton commentedon Dec 1, 2021
This was fixed in TypeScript 4.0: https://www.typescriptlang.org/play?target=99&ts=4.0.5#code/C4JwngBA3gUAkAQwO4IJbAgCgQZzAOwGMsBKCAXgD5oBfEzEgbhhokIWEIAtp4B6PixgwApgA8ADgHsQGKDUZA
rbuckton commentedon Dec 1, 2021
Outside of an async function or the top level of a module,
awaitis considered an identifier, so the error isn't wrong, per se. We can consider introducing a better error message for this specific case, considering you might have meant to declare the function asasync. We already do that if your operand is not parenthesized:function f() { await 1; }gives you an error and a quick fix to "Add async modifier to containing function". We just don't currently offer that quick fix when we initially parsed it as a call expression.rbuckton commentedon Dec 1, 2021
I created #46986 to track the possibility of adding a distinct error message for an ambiguous
await, along with providing the relevant quick fix.