简体   繁体   English

如何设置 VSCode 以内联显示 typescript 错误

[英]How to setup VSCode to show typescript error inline

I'm trying to migrate an ES6 project to typescript. This is my first attempt at writing a typescript module in NodeJS.我正在尝试将 ES6 项目迁移到 typescript。这是我第一次尝试在 NodeJS 中编写 typescript 模块。

It seems to work better in Angular-CLI so far.到目前为止,它似乎在 Angular-CLI 中工作得更好。

I've got it compiling using command line tsc , but I'm not sure how to display errors and intellisense in the code editor?我已经使用命令行tsc进行了编译,但我不确定如何在代码编辑器中显示错误和智能感知?

In a directory I have the two files shown below.在一个目录中,我有如下所示的两个文件。 When it compiles it, as expected throws a compile error: Supplied parameters do not match any signature of cal l target.当它编译它时,正如预期的那样抛出一个编译错误: Supplied parameters do not match any signature of cal l target. . . This is fine.这可以。

But VSCode is not showing any errors in the editor, even if I make a deliberate syntax error like taking out a bracket, or type errors like this one.但是 VSCode 没有在编辑器中显示任何错误,即使我故意犯了一个语法错误,比如去掉一个括号,或者像这样输入错误。 How do I get VSCode to show inline syntax or compiler errors in the editor for.ts files?如何让 VSCode 在 .ts 文件的编辑器中显示内联语法或编译器错误?

validation-error.ts验证错误.ts

/**
 * Wrapper for a particular validation error.
 */
export class ValidationError extends Error {
  private type: string;

  constructor(error: any) {
    super(error);
    Error.captureStackTrace(this, this.constructor);
    this.message = error.message;
    this.type = 'ValidationError';
  }
}

Then I'm trying to write simple spec to test out the workflow:然后我尝试编写简单的规范来测试工作流程:

validation-error.spec.ts验证错误.spec.ts

import { ValidationError } from './validation-error';
import * as should from 'should';

describe('Validation Error', () => {
  it('should create a new instance', () => {
    const instance = new ValidationError();
    should.exist(instance);
  });
});

Edited Here:在这里编辑:

I'm still plugging away at this - I've setup tsc to run automatically with this job in tasks.json :我仍在努力解决这个问题——我已经将tsc设置为在tasks.json中自动运行这项工作:

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "taskName": "tsc",
  "command": "tsc",
  "isShellCommand": true,
  "args": ["-w", "-p", "."],
  "problemMatcher": "$tsc-watch",
  "echoCommand": true
}

I suspect that if the errors were reported properly using $tsc-watch , the error would probably also show up in the editor.我怀疑如果使用$tsc-watch正确报告了错误,该错误也可能会显示在编辑器中。 When I run the task I get output like:当我运行任务时,我得到 output,例如:

running command$ tsc -w -p .
src/hello.ts(15,1): error TS2346: Supplied parameters do not match any signature of call target.
4:24:40 PM - Compilation complete. Watching for file changes.

But there are no problems reported in the Problems view - even though clearly there's a compiler issue.但是Problems视图中没有报告任何问题——尽管显然存在编译器问题。

Got the $tsc-watch setting from this page of the docs: https://code.visualstudio.com/docs/editor/tasks从此文档页面获得$tsc-watch设置: https://code.visualstudio.com/docs/editor/tasks

For me the problem was resolved by setting Typescript validation to true in VSCode settings:对我来说,问题是通过在 VSCode 设置中将 Typescript 验证设置为 true 来解决的:

"typescript.validate.enable": true,

I had turned this off in my workspace settings in the past and forgot!过去我在我的工作区设置中关闭了它并忘记了!

So make sure to double check that this setting is not set to false in your user settings, workspace settings or folder settings .因此,请确保在您的用户设置、工作区设置或文件夹设置中仔细检查此设置未设置为 false。

在此处输入图片说明

For instance the rule I was trying to get work was checking for undefined variables which was immediately fixed when I set validate to true.例如,我试图开始工作的规则是检查未定义的变量,当我将 validate 设置为 true 时立即修复。

在此处输入图片说明

Even without having installed TypeScript locally or globally, the following setup provides inline errors.即使没有在本地或全局安装 TypeScript,以下设置也会提供内联错误。

C:/temp
  index.ts
  tsconfig.json

index.ts索引.ts

let x: number;
x = "foo";

tsconfig.json配置文件

{ }

Here is a screenshot that shows the inline error in x .这是一个屏幕截图,显示了x中的内联错误。

在此处输入图片说明

Please try that setup and report back on what happens.请尝试该设置并报告发生的情况。

I just closed & restarted my VS Code and it started showing typescript errors like before.我刚刚关闭并重新启动了我的 VS Code,它开始像以前一样显示打字稿错误。

If this doesn't work, I'd suggest restarting your computer system.如果这不起作用,我建议重新启动您的计算机系统。

I ran aground with this issue, managed to solve it by adding "typescript.validate.enable": true to my local settings.json file in the .vscode directory.我遇到了这个问题,设法通过向 .vscode 目录中的本地 settings.json 文件添加"typescript.validate.enable": true来解决它。 My settings tell me that the typescript validator is enabled for VS code globally, but I wasn't able to see inline TS errors until I added the local settings file.我的设置告诉我打字稿验证器已为 VS 代码全局启用,但在我添加本地设置文件之前,我无法看到内联 TS 错误。

The other answers (most before year 2020) did not work for me.其他答案(大多数在 2020 年之前)对我不起作用。

According to this feature request in Microsoft's VS Code repository, do the following steps:根据 Microsoft VS Code 存储库中的此功能请求,执行以下步骤:

  1. In your settings.json add:在你的 settings.json 添加:
"typescript.validate.enable": true,
"typescript.tsserver.experimental.enableProjectDiagnostics": true,
  1. In your tsconfig.json add:在你的 tsconfig.json 添加:
"include": [
    "src",
    "global.d.ts",
  ],
"exclude": [
    "node_modules",
    "plugins",
    "public"
  ]

Wait a while (or restart VS Code) in order to see a list of errors shown in the open files and added in the problems panel.稍等片刻(或重新启动 VS Code),以便在打开的文件中看到错误列表并添加到问题面板中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置 TypeScript、VScode、Yarn2、Fastify 项目 - How to setup TypeScript, VScode, Yarn2, Fastify project 如何在vscode错误中调试打字稿节点应用程序 - how to debug a typescript node application in vscode error 如何在 vscode 中禁用错误突出显示 typescript 代码 - How to disable error highlighting typescript code in vscode 如何去除vscode中的红色下划线错误? (打字稿) - how to remove red underline error in vscode? (typescript) VSCode 未显示内联 typescript 错误 - VSCode isn't showing inline typescript errors 如何让 VSCode 显示 TypeScript 文件*未*在编辑器中打开的错误? - How to get VSCode to show TypeScript errors for files *not* open in the editor? 如何让 VSCode 向我显示 strictNullChecks Typescript 错误 - How to make VSCode show me strictNullChecks Typescript errors 如何使用打字稿通过切换按钮显示html表的内联详细信息 - How to show inline details for html table with a toggle button using typescript Vue 3 + TypeScript:使用 JS 扩展运算符从 setup() 返回的对象在 vscode 中引发错误 - Vue 3 + TypeScript: Objects returned from setup() using the JS spread operator throw an error in vscode VSCode 如何修复 typescript 突出显示错误 {} 类型的参数不可分配给 - VSCode how to fix typescript highlight error Argument of type {} is not assignable to parameter of
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM