Skip to content

[3.8] [BUG] TLA top-level await breaks with parenthesis #36817

Description

@trusktr

TypeScript Version: 3.8.1-rc

Search Terms:

Code

// works:
const DBService = await import('./DBService')
await DBService.start()

// doesn't work
await (await import('./DBService')).start()
await (1 + 1)

// works:
async function foo() {
    await (await import('./DBService')).start()
}

export {}

Expected behavior:

no error on the await word

Actual behavior:

TS thinks it is an identifier.

Playground Link:

Playground link

Related Issues:

#35276

Activity

rbuckton commented on Apr 8, 2020

@rbuckton
Contributor

This is a consequence of how we parse source files and determine whether they are a Module or a Script based on the presence or absence of import or export declarations. According to the TLA proposal, await is now treated like a keyword at the top level of a Module, but not a Script. The fact that await p (without parens) works at the top level currently is actually due to the fact we would parse it as an AwaitExpression for error reporting purposes (and still do for await in a normal function).

To address this, we will likely need to "reparse" a function call whose expression is an await and replace it with an AwaitExpression in any top-level position.

rbuckton commented on Apr 8, 2020

@rbuckton
Contributor

We are discussing the details necessary to make this work, but it may depend on changes to our AST Node Factory API proposed in #35282

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 TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions