prisma-examples
prisma-examples copied to clipboard
Remove top-level async function in TS script examples
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.
I couldn't get this to work while exploring this, here's what I tried:
- Simply remove the
main
function from thetypescript/script
example- Top-level
await
doesn't work
- Top-level
- 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
- Top-level
- 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 aCannot find module '@prisma/client'.ts(2307)
error for the import statement of Prisma Client
- Top-level
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?
https://github.com/prisma/prisma-client-js/issues/540 is related as it makes calling prisma.disconnect()
optional.
Related Tweet by @jedmao: https://twitter.com/mrjedmao/status/1250680713038479360
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:
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.
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.
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?
This playground appears to work:
https://www.typescriptlang.org/play/index.html#code/IYd2EsBcAIAUCcD2BbcBnApgOnhtiAbANwwAoBKAbgCgMAPAB0XhgG9oBfSoA
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.
Finally, demystified!