Ngu*_*HCM 13 node.js typescript top-level-await
我正在研究 Typescript 并遇到问题。我想使用import
和 顶级await
,但目前我一次只能使用一个。
这是我的配置,tsconfig.json
允许我使用导入
"target": "ESNext",
"module": "ESNext"
Run Code Online (Sandbox Code Playgroud)
这个允许我使用顶级等待:
"module":"commonjs"
Run Code Online (Sandbox Code Playgroud)
我添加了
"type":"module"
Run Code Online (Sandbox Code Playgroud)
给我的package.json
有什么方法可以同时利用这两个功能吗?非常感谢。
我正在使用 Nodejs 16 和 Typescript 4.5.5
是的,您可以同时使用两者。以下信息可帮助您重现成功的测试:
Node.js (LTS) 版本:
$ node --version
v16.14.0
Run Code Online (Sandbox Code Playgroud)
./package.json
:
$ node --version
v16.14.0
Run Code Online (Sandbox Code Playgroud)
./tsconfig.json
:
{
"name": "so-71099311",
"version": "0.1.0",
"private": true,
"description": "",
"type": "module",
"scripts": {
"compile": "tsc",
"start": "npm run compile && node dist/main.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"@types/node": "^17.0.17",
"typescript": "^4.5.5"
}
}
Run Code Online (Sandbox Code Playgroud)
./src/module.ts
:
{
"compilerOptions": {
"esModuleInterop": true,
"exactOptionalPropertyTypes": true,
"isolatedModules": true,
"lib": [
"esnext"
],
"module": "esnext",
"moduleResolution": "node",
"noUncheckedIndexedAccess": true,
"outDir": "dist",
"strict": true,
"target": "esnext",
"useUnknownInCatchVariables": true
},
"include": [
"./src/**/*"
]
}
Run Code Online (Sandbox Code Playgroud)
./src/main.ts
:
export function greet (name = 'world'): void {
console.log(`Hello ${name}`);
}
Run Code Online (Sandbox Code Playgroud)
在你的控制台中:
$ cd /path/to/the/directory/where/you/created/these/files
$ npm install
$ npm run start
Run Code Online (Sandbox Code Playgroud)
> so-71099311@0.1.0 start
> npm run compile && node dist/main.js
> so-71099311@0.1.0 compile
> tsc
Hello Nguyen
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10659 次 |
最近记录: |