How can I specify TypeScript Module and Target in `.js` files?

Viewed 27

I have a .mjs file:

console.log(await foo())

I want this code to be type-checked (there's more in this file of course ;) ) so I add // @ts-check

// @ts-check
console.log(await foo())

but now tsserver shows me this error:

Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.

How can I fix this?

Solutions I have thought of:

  1. adding a jsconfig.json to my project
  • I don't want any extra files in my project
  1. Don't use top-level await
  • In this case I could just do foo().then(console.log)

Ideal solution

A comment I can place next to @ts-check like

// @ts-check
// @ts-module es2022
// @ts-target es2022

console.log(await foo())
0 Answers
Related