TypeScript: Top-level `await` error message is extremely unclear

// @target: es2017
// @module: esnext

await 100;

This gives the following error message:

'await' outside of an async function is only allowed at the top level of a module when '--module' is 'esnext' or 'system' and '--target' is 'es2017' or higher.

This is really unclear! My compiler options are all set correctly, so why is this not working?

It’s because it’s not a module. This error message is expecting me to meet 3 different conditions, “top level of a module” is unclear for any user who just wants to use this feature. They’ll likely take “module” to just mean “file”.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 15
  • Comments: 19 (15 by maintainers)

Most upvoted comments

Personally, I’d prefer just one at a time, but both are fine at once.

I’m generally not a fan of fixing one error just to end up with another error when the fix had nothing to do with the second error. I’d rather get both so I know all the actions I need to take.

Should we have a codefix for the first one to add an export {} to the file?

Also, have we considered adding codefixes to fix the tsconfig.json for things like the 2nd error message?

It fixed for me when I changed "module": "ESNext" to "module": "es2022".