prisma-examples icon indicating copy to clipboard operation
prisma-examples copied to clipboard

Remove top-level async function in TS script examples

Open nikolasburk opened this issue 4 years ago • 9 comments

TS now supports top-level await: https://twitter.com/typescript/status/1230629807991341058?s=12

We should consider removing the top-level async main function in the TS script example.

image

nikolasburk avatar Feb 25 '20 08:02 nikolasburk

I couldn't get this to work while exploring this, here's what I tried:

  1. Simply remove the main function from the typescript/script example
    • Top-level await doesn't work
  2. TS release notes say this only works in a module, so added export {} to the end of the file
    • Top-level await still doesn't work
  3. Adjust tsconfig to add (as explained in the docs):
    {
      "compilerOptions": {
        "sourceMap": true,
        "outDir": "dist",
        "strict": true,
        "lib": ["esnext", "dom"],
        "esModuleInterop": true,
    +   "target": "es2017",
    +   "module": "esnext"
      }
    }
    
    • Top-level await still doesn't work plus I'm getting a Cannot find module '@prisma/client'.ts(2307) error for the import statement of Prisma Client

Also, I want to re-raise Luca's point from the Slack discussion: How do we plan to catch exceptions that are potentially being raised?

nikolasburk avatar Mar 02 '20 09:03 nikolasburk

https://github.com/prisma/prisma-client-js/issues/540 is related as it makes calling prisma.disconnect() optional.

schickling avatar Mar 02 '20 15:03 schickling

Related Tweet by @jedmao: https://twitter.com/mrjedmao/status/1250680713038479360

schickling avatar Apr 16 '20 08:04 schickling

Just tried to adjust my tsconfig.json according to the tweet:

{
  "compilerOptions": {
    "sourceMap": true,
    "outDir": "dist",
    "strict": true,
    "lib": ["esnext"],
    "esModuleInterop": true,
    "module": "ESNext",
    "target": "ES2019",
  },
  "include": ["node_modules/@prisma/client"]
}

Even though the options seem to be configured properly, I get red squiggly lines in VS Code:

image

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

Oddly when I run this I get a different error:

$ yarn dev
yarn run v1.22.0
$ ts-node ./script.ts

/Users/nikolasburk/Desktop/prisma-examples/typescript/script/node_modules/ts-node/src/index.ts:421
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
script.ts:1:30 - error TS2307: Cannot find module '@prisma/client'.

1 import { PrismaClient } from '@prisma/client'
                               ~~~~~~~~~~~~~~~~

    at createTSError (/Users/nikolasburk/Desktop/prisma-examples/typescript/script/node_modules/ts-node/src/index.ts:421:12)
    at reportTSError (/Users/nikolasburk/Desktop/prisma-examples/typescript/script/node_modules/ts-node/src/index.ts:425:19)
    at getOutput (/Users/nikolasburk/Desktop/prisma-examples/typescript/script/node_modules/ts-node/src/index.ts:552:36)
    at Object.compile (/Users/nikolasburk/Desktop/prisma-examples/typescript/script/node_modules/ts-node/src/index.ts:757:32)
    at Module.m._compile (/Users/nikolasburk/Desktop/prisma-examples/typescript/script/node_modules/ts-node/src/index.ts:836:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:770:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/nikolasburk/Desktop/prisma-examples/typescript/script/node_modules/ts-node/src/index.ts:839:12)
    at Module.load (internal/modules/cjs/loader.js:628:32)
    at Function.Module._load (internal/modules/cjs/loader.js:555:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:822:10)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I deleted and re-installed node_modules but am still seeing the same error.

nikolasburk avatar Apr 16 '20 08:04 nikolasburk

I had this working yesterday, but somehow it's giving me the red squiggly today 🤷

The error message isn't helpful, as the compiler options are already configured accordingly. Maybe it's a TypeScript issue.

jednano avatar Apr 16 '20 14:04 jednano

I just tried to get it working with a very basic example without Prisma Client - even that doesn't work with the latest typescript@next.

https://github.com/timsuchanek/top-level-await

Do you know of any running TypeScript top-level-await examples out there?

timsuchanek avatar Apr 17 '20 07:04 timsuchanek

This playground appears to work:

https://www.typescriptlang.org/play/index.html#code/IYd2EsBcAIAUCcD2BbcBnApgOnhtiAbANwwAoBKAbgCgMAPAB0XhgG9oBfSoA

jednano avatar Apr 17 '20 14:04 jednano

Update about top-level await: I just talked to Andrew from the TypeScript team, and it looks like it’s not yet what we think it is.

So what this PR just did, is making sure, that top level await is “not transformed” anymore. That means, if you have top-level await in your TS code, it will end up in the generated JS code.

In order to use it today, you need to provide the v8 flag --harmony-top-level-await. Another option he mentioned, is that you might get it running with --module=systemjs, which we can investigate, but it doesn't seem like an established thing yet, that we should recommend as the default.

So it seems like Node is just not ready yet.

timsuchanek avatar Apr 22 '20 09:04 timsuchanek

Finally, demystified!

jednano avatar Apr 22 '20 19:04 jednano