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

Top-level await doesn't work with ts-node typescript files. #580

Closed
Pokute opened this issue Aug 22, 2018 · 7 comments
Closed

Top-level await doesn't work with ts-node typescript files. #580

Pokute opened this issue Aug 22, 2018 · 7 comments
Labels

Comments

@Pokute
Copy link

Pokute commented Aug 22, 2018

.esmrc: { "await": true }

topAwait.js|ts:console.log(await (new Promise((resolve) => resolve('ok'))));

Above code works if it's run with both node -r esm topAwait.js or ts-node -r esm topAwait.js.

if run with ts-node -r esm topAwait.ts, It fails with topAwait.ts(1,13): error TS2304: Cannot find name 'await'.

@jdalton
Copy link
Member

jdalton commented Aug 22, 2018

Hi @Pokute!

For ts-node you'll need to do some more ts-node config. See this comment.

@jdalton jdalton closed this as completed Aug 22, 2018
@Pokute
Copy link
Author

Pokute commented Aug 22, 2018

Well spotted, I managed to get it to work with following:

export TS_NODE_IGNORE_DIAGNOSTICS=1308,2304
node -r esm -r ts-node/register topAwait.ts

the order of requires is crucial. For the same reason ts-node -r esm topAwait.ts fails.

Is might be possible to set that global ignore or something similar for TS1308 from esm if top-level await is enabled before ts-node/register is activated since it's very specific to this. Unfortunately TS2304 is more difficult since Cannot find name 'await' is the same error for all unrecognised symbols.

@jdalton
Copy link
Member

jdalton commented Aug 22, 2018

Did you set the TS target to es2017?

@Pokute
Copy link
Author

Pokute commented Aug 22, 2018

Target doesn't matter, as long as it's at least es5.

@jdalton
Copy link
Member

jdalton commented Aug 22, 2018

Based on the comment from the linked issue:

Set target to "es2017" or higher. Otherwise an awaiter is generated by TypeScript and await is rewritten to yield

I'm not a TS user so I don't know if this still holds true.

@pitust
Copy link

pitust commented Aug 27, 2020

it still does

@jakobrosenberg
Copy link

await () seems to be interpreted as a function.

If I remove the parenthesis I get this instead

SyntaxError: await is only valid in async functions and the top level bodies of modules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants