Skip to content

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

Closed

Description

@kitsonk

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

Activity

  1. lissein commented on May 12, 2020

    @lissein
    Contributor

    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.

  2. Shayan-To commented on Sep 4, 2021

    @Shayan-To

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

  3. rbuckton commented on Dec 1, 2021

    @rbuckton
    Contributor
  4. rbuckton commented on Dec 1, 2021

    @rbuckton
    Contributor

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

  5. rbuckton commented on Dec 1, 2021

    @rbuckton
    Contributor

    I created #46986 to track the possibility of adding a distinct error message for an ambiguous await, along with providing the relevant quick fix.

  6. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions