vscode无法编译打字稿文件

时间:2020-05-11 01:11:32

标签: typescript visual-studio-code

我发现vscode无法编译我的打字稿代码,即使我严格按照他们的教程进行操作: https://code.visualstudio.com/docs/typescript/typescript-compiling

在完成了上面教程的所有配置后,当我单击“运行->运行而不进行调试”时,我得到了:

无法启动程序'/Users/username/Desktop/work/ts/main.ts' 因为找不到对应的JavaScript。

但是,如果我跑步

tsc

在终端上运行,然后“运行->运行而无需调试”即可。

“ main.ts”:

...
WorkingDirectory=/var/www/project/
ExecStart=/usr/bin/dotnet /var/www/project/project.dll
...

tsconfig.json:

  let a : Array<number> = [1,2,3]
  let b : number[] = a
  console.log(a === b)

launch.json:

{
    "compilerOptions": {
      "target": "es5",
      "module": "esnext",
      "outDir": "out",
      "sourceMap": true,
    }
}

tasks.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}/main.ts",
            "outFiles": [
                "${workspaceFolder}/**/*.js"
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

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"]
}