typescript 如何设置 VSCode 内联显示打字稿错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43908813/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:32:30  来源:igfitidea点击:

How to setup VSCode to show typescript error inline

typescriptvisual-studio-code

提问by Richard G

I'm trying to migrate an ES6 project to typescript. This is my first attempt at writing a typescript module in NodeJS.

我正在尝试将 ES6 项目迁移到打字稿。这是我第一次尝试在 NodeJS 中编写打字稿模块。

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.. This is fine.

在一个目录中,我有两个文件如下所示。当它编译它时,正如预期的那样抛出一个编译错误:Supplied parameters do not match any signature of cal l target.。这可以。

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. How do I get VSCode to show inline syntax or compiler errors in the editor for .ts files?

但是 VSCode 没有在编辑器中显示任何错误,即使我故意犯了一个语法错误,比如去掉括号,或者输入这样的错误。如何让 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

验证-error.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 tscto 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. When I run the task I get output like:

我怀疑如果使用 正确报告错误$tsc-watch,该错误可能也会显示在编辑器中。当我运行任务时,我得到如下输出:

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 Problemsview - even though clearly there's a compiler issue.

但是Problems视图中没有报告任何问题- 即使显然存在编译器问题。

Got the $tsc-watchsetting from this page of the docs: https://code.visualstudio.com/docs/editor/tasks

$tsc-watch从文档的此页面获取设置:https: //code.visualstudio.com/docs/editor/tasks

采纳答案by Shaun Luttin

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.

enter image description here

在此处输入图片说明

Please try that setup and report back on what happens.

请尝试该设置并报告发生的情况。

回答by protoEvangelion

For me the problem was resolved by setting Typescript validationto 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 。

enter image description here

在此处输入图片说明

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.

例如,我试图开始工作的规则是检查未定义的变量,当我将验证设置为 true 时,该规则立即修复。

enter image description here

在此处输入图片说明

回答by Jamshed

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.

如果这不起作用,我建议您重新启动计算机系统。