Skip to content
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

Incorrect lexing of TLA await in try...catch #38483

Closed
kitsonk opened this issue May 11, 2020 · 5 comments
Closed

Incorrect lexing of TLA await in try...catch #38483

kitsonk opened this issue May 11, 2020 · 5 comments
Assignees
Labels
Bug A bug in TypeScript Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@kitsonk
Copy link
Contributor

kitsonk commented May 11, 2020

TypeScript Version: 3.8.3

Search Terms: top level await, try catch

Code

try {
  await (async () => {})(); // TS2304 cannot find name 'await'
} catch {
  //
}

Expected behavior:

The file is considered valid.

Actual behavior:

The file has an error at await of "TS2304 cannot find name 'await'".

Playground Link: https://www.typescriptlang.org/play?#code/C4JwngBA3gUAkAQwO4IJbAgCgQZzAOwGMsBKCAXgD5oBfEzEgbhhokIWEIAtp4B6PixhA

@lissein
Copy link
Contributor

lissein commented 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
Copy link

Shayan-To commented Sep 4, 2021

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...

@rbuckton
Copy link
Member

rbuckton commented Dec 1, 2021

This was fixed in TypeScript 4.0: https://www.typescriptlang.org/play?target=99&ts=4.0.5#code/C4JwngBA3gUAkAQwO4IJbAgCgQZzAOwGMsBKCAXgD5oBfEzEgbhhokIWEIAtp4B6PixgwApgA8ADgHsQGKDUZA

@rbuckton
Copy link
Member

rbuckton commented Dec 1, 2021

[...] 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.

@rbuckton
Copy link
Member

rbuckton commented 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.

@rbuckton rbuckton closed this as completed Dec 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants