我发现vscode无法编译我的typescript代码,即使我完全遵循了他们的教程:https://code.visualstudio.com/docs/typescript/typescript-compiling
在完成了上面教程的所有配置后,当我点击"Run->Run without Debugging"时,我得到了:
无法启动程序'/Users/username/Desktop/work/ts/main.ts'因为找不到相应的JavaScript。
但是,如果我运行
tsc
在终端上,然后"Run->Run without Debugging"工作。
"main.ts":
let a : Array<number> = [1,2,3]
let b : number[] = a
console.log(a === b)
tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"outDir": "out",
"sourceMap": true,
}
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/main.ts",
"outFiles": [
"${workspaceFolder}/**/*.js"
]
}
]
}
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": [
"$tsc"
],
"group": {
"kind": "build",
"isDefault": true
},
"label": "tsc: build - tsconfig.json"
}
]
}
在tasks.json
中需要更改标签,例如"label": "build"
在launch.json
中,我们可以使用preLaunchTask属性-"preLaunchTask": "build"
无论是否运行调试,都应该开始构建并保持监视。构建任务本身也应通过Ctrl+Shift+B
工作
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/main.ts",
"preLaunchTask": "build",
"outFiles": [
"${workspaceFolder}/out/**/*.js"
]
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": [
"$tsc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
此外,您可能希望排除一些目录
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"outDir": "out",
"sourceMap": true,
},
"exclude": ["**/node_modules/*", "out", "coverage"]
}