如何使VSCode显示未在编辑器中打开的文件的TypeScript错误? [英] How to get VSCode to show TypeScript errors for files *not* open in the editor?

查看:65
本文介绍了如何使VSCode显示未在编辑器中打开的文件的TypeScript错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 create-react-app@2.1.8 TypeScript/React应用程序中,TS编译器错误仅显示在问题"窗格中,显示了我在VS Code编辑器中打开的文件.结果,有时我直到真正运行该应用程序时才发现错误.

In my create-react-app@2.1.8 TypeScript/React app, TS compiler errors are only shown in the Problems pane for files I have open in the VS Code editor. As a result I sometimes don't catch errors until I actually run the app.

打开工作区时或保存对文件的更改后,如何让TypeScript自动检查所有代码文件中的编译器错误?

How can I get TypeScript to automatically check for compiler errors in all code files as soon as I open the workspace, or when I save changes to a file?

我尝试将"watch":"tsc --watch" 作为脚本添加到 package.json 中,然后进行 npm运行脚本监视在集成的终端窗格中,但这有两个问题:

I've tried adding "watch": "tsc --watch" as a script in package.json and then doing npm run-script watch in an integrated terminal pane, but this has two problems:

  • 错误显示在终端窗格中,但未在VSCode中填充问题"窗格
  • 我必须手动运行它,而不是在工作区加载时自动启动

有更好的解决方案吗?

顺便说一句,这与>显示项目范围内的TypeScript问题相同/errors in webstorm ,但关于Visual Studio Code而不是webstorm.

BTW, this is the same question as Show project wide TypeScript problems/errors in webstorm, but about Visual Studio Code instead of webstorm.

推荐答案

不幸的是,截至2019年11月,VS Code不支持此功能.参见 https://github.com/Microsoft/vscode/issues/13953 .

As of November 2019, unfortunately this isn't supported by VS Code out of the box. See https://github.com/Microsoft/vscode/issues/13953.

我能找到的最佳解决方法是将一个任务添加到task.json中,该任务将运行 tsc --watch ,并将其配置为在打开工作区或文件夹时运行.

The best workaround that I could find is to add a task to tasks.json that will run tsc --watch, and configure it to run when the workspace or folder is opened.

以下是task.json的示例配置.请注意,以下代码段中的"www"是指您要检查的具有 tsconfig.json 的文件夹.相对于工作区的根目录,将其替换为您自己的文件夹的名称.

Below is a sample config for tasks.json. Note that "www" in the snippet below refers to the folder with tsconfig.json that you want to check. Replace it with the name of your own folder, relative to the root of your workspace.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "tsc watch",
            "type": "shell",
            "command": "./node_modules/.bin/tsc",
            "isBackground": true,
            "args": ["--watch", "--noEmit", "--project", "www"],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal": "never",
                "echo": false,
                "focus": false,
                "panel": "dedicated"
            },
            "problemMatcher": "$tsc-watch",
            "runOptions": {
                "runOn": "folderOpen"
            }
        }
    ]
}

请注意,默认情况下似乎未启用自动运行任务.要选择自动运行任务,请按照此处的说明进行操作: https://stackoverflow.com/a/58866185/126352

Note that auto-run tasks don't seem to be enabled by default. To opt into auto-run tasks, follow the instructions here: https://stackoverflow.com/a/58866185/126352

此解决方案改编自上面链接的GitHub问题中的 @molinx 的答案,并对其进行了改进,谢谢来 @ kumar303 在下面的评论.

This solution was adapted from @molinx's answer in the GitHub issue linked above, and it was improved thanks to @kumar303's comment below.

这篇关于如何使VSCode显示未在编辑器中打开的文件的TypeScript错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆