typescript 如何让 Visual Studio Code 检查整个项目是否有错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41702815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make Visual Studio Code check entire project for errors?
提问by WillyC
I'm using VS Code for TypeScript/JavaScript development. When I open a file it will check that file for errors. The problem is if I'm refactoring (like I move some shared code to a new location or change a name) it won't show me the errors this caused until I open the file with the problem. ...so if I want to do extensive refactoring I have to open every file just to make it scan the file for errors.
我正在使用 VS Code 进行 TypeScript/JavaScript 开发。当我打开一个文件时,它会检查该文件是否有错误。问题是如果我正在重构(比如我将一些共享代码移动到新位置或更改名称),它不会向我显示由此导致的错误,直到我打开有问题的文件。...因此,如果我想进行广泛的重构,我必须打开每个文件才能使其扫描文件中的错误。
How can I make VS Code scan the whole project for errors without having to open each file one by one manually?
如何让 VS Code 扫描整个项目中的错误,而不必手动一个一个地打开每个文件?
采纳答案by WillyC
Figured it out. Note this answer is specific to TypeScript, which is what I am using. Here it is:
弄清楚了。请注意,此答案特定于我正在使用的 TypeScript。这里是:
Make sure typescript is installed globally (I just had mine installed locally apparently):
npm install -g typescript
确保打字稿是全局安装的(我只是在本地安装了我的显然):
npm install -g typescript
Then in VS Code press Shift+Ctrl+B. If you don't have a task runner set up it will ask what you want. I selected typescript and the tasks.json file will look like this:
然后在 VS Code 中按Shift+ Ctrl+ B。如果您没有设置任务运行器,它会询问您想要什么。我选择了 typescript,tasks.json 文件将如下所示:
{
"version": "0.1.0",
"command": "tsc",
"isShellCommand": true,
"args": ["-p", "."],
"showOutput": "silent",
"problemMatcher": "$tsc"
}
Then pressing Shift+Ctrl+B(or Shift+Command+Bin macOS) will check the entire project for problems and they will be reported in your "problems" panel.
然后按Shift+ Ctrl+ B(或macOS 中的Shift+ Command+ B)将检查整个项目是否存在问题,它们将在您的“问题”面板中报告。
回答by manoftheyear
For the most recent versionof tasks.json
this is the correct json, following deprecations in version 1.14. Create this as /.vscode/tasks.json
对于最新版本,tasks.json
这是正确的 json,遵循 1.14 版中的弃用。将其创建为/.vscode/tasks.json
{
"version": "2.0.0",
"command": "tsc",
"type": "shell",
"args": [
"-p",
"."
],
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$tsc"
}
回答by ford04
VS Code (v1.44) has an experimentalfeature, that allows project wide error reportingin TS. Try it out:
VS Code (v1.44) 具有实验性功能,允许在 TS 中报告项目范围的错误。试试看:
// put this line in settings.json
"typescript.tsserver.experimental.enableProjectDiagnostics": true
回答by Jan Aagaard
If you don't want to install TypeScript globally, you can do the following:
如果不想全局安装 TypeScript,可以执行以下操作:
- Add a validate-typescript run script to
./package.json
.--noEmit
means that the compiler will won't generate any JavaScript files.
- 将验证打字稿运行脚本添加到
./package.json
.--noEmit
意味着编译器不会生成任何 JavaScript 文件。
{
"scripts": {
"validate-typescript": "tsc --noEmit"
}
}
- Let VSCode know about the run script in
/.vscode/tasks.json
.
- 让 VSCode 了解
/.vscode/tasks.json
.
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "validate-typescript",
"problemMatcher": [
"$tsc"
]
}
]
}
- To run the tasks hit the F1 key and select 'Run Task', and then 'npm: validate-typescript'.
- 要运行任务,请按 F1 键并选择“运行任务”,然后选择“npm:validate-typescript”。
回答by AriElsa
Once you have open your project in vs code, open the vs code terminal and run:
在 vs code 中打开项目后,打开 vs code 终端并运行:
node_modules/.bin/tsc --noEmit