是的,你可以同时使用两者。以下是帮助您重现成功测试的信息:
Node.js(LTS)版本:
$ node --version
v16.14.0
./package.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"
}
}
./tsconfig.json
:
{
"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/**/*"
]
}
./src/module.ts
:
export function greet (name = 'world'): void {
console.log(`Hello ${name}`);
}
./src/main.ts
:
import {greet} from './module.js';
const name = await Promise.resolve('Nguyen');
greet(name);
在你的控制台中:
$ cd /path/to/the/directory/where/you/created/these/files
$ npm install
$ npm run start
> so-71099311@0.1.0 start
> npm run compile && node dist/main.js
> so-71099311@0.1.0 compile
> tsc
Hello Nguyen
'./module.js'
,但应该是'./module.ts'
。而你不能将导入改为以.ts
结尾,因为不允许这样做。你也不能完全删除.js
,因为有"type": "module"
。这是一个进退两难的局面。 - leonheess.js
结尾的特定符号,这些符号是本地模块,将其相应地转换为.ts
对应项。 - jsejcksndist
目录下。无法找到模块“./module.js”或其对应的类型声明。 - leonheessnode --version && cat -b ./package.json ./tsconfig.json ./src/module.ts ./src/main.ts && tsc --traceResolution
- jsejcksn