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

Remove extraneous top-level await requirement #37794

Closed
Vandivier opened this issue Apr 4, 2020 · 4 comments
Closed

Remove extraneous top-level await requirement #37794

Vandivier opened this issue Apr 4, 2020 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@Vandivier
Copy link

This may be a bug, I'm not sure. So I'm submitting as a feature request.

Search Terms

await, top-level, disable rule

Suggestion

"isolatedModules": true, should not be required to remove top-level await error.

Use Cases

I'm scripting a very simple file I/O script using Deno. The script runs without issue. Full code is in the below example.

Even though it works, VS Code complains. I think the complaint is at the TS level and I can't ignore it as a specific error. I can ts-ignore line-by-line, but that's not satisfactory.

The error is 'await' expressions are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.ts(1375)

Examples

const decoder = new TextDecoder("utf-8");
const data = await Deno.readFile("tsconfig.json"); // <!-- VS Code shouldn't have red squiggle here.
console.log(decoder.decode(data));

Checklist

I have no idea whether this feature meets the items in the checklist.

Related Issues

  1. [merged PR] Add top-level await for esnext and system modules #35813
  2. @ts-ignore for the block scope and imports #19573
  3. Top Level "for await" not supported, but should be #37402
  4. Support top level "for await of" #37424
  5. [closed] Top-level await error message is extremely unclear #36036
@kitsonk
Copy link
Contributor

kitsonk commented Apr 5, 2020

@Vandivier TLA await are invalid in script files. Deno loads every file as an ESM file, but to VSCode your example doesn't look like a module, so it it is treating it like a script. The current canonical way to for a file to be a module instead of a script when there are no other imports or exports is to include an export {} in the file. This will resolve the issue.

There is a proposal for a pragma to force modules, but it is Stage 1 (early stages) in TC39.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jun 1, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

@avin-kavish
Copy link

avin-kavish commented Aug 10, 2022

@kitsonk We now have a way to know whether files are implicitly to be treated as modules though, i.e. type: "module". So can we consider this?

Not sure why target in tsconfig.json alone is not enough though. If it's set to a module, all files get compiled into modules.

@RyanCavanaugh
Copy link
Member

Addressed at #36036 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants