typescript 如何在 VS Code 中使用 TSLint?

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

How can I use TSLint in VS Code?

typescriptvisual-studio-codetslint

提问by Zen

I have installed TSLint in VSCode and created a tslint.jsonfile next to tsconfig.json. But TSLint is not working. For example, I added "curly": trueto tslint.json, but when I write a if statement without curly braces, VS Code doesn't give any warning. What does this extension do?

我在VSCode安装TSLint并创建了tslint.json一个文件来tsconfig.json。但是 TSLint 不起作用。例如,我添加了"curly": trueto tslint.json,但是当我编写没有花括号的 if 语句时,VS Code 不会给出任何警告。这个扩展有什么作用?

enter image description here

在此处输入图片说明

采纳答案by Markus Klug

The vscode-tslint extension currently crashes silently when it encounters an invalid config-option. In my case, it was the no-trailing-commarule which has to be changed to trailing-comma.

vscode-tslint 扩展当前在遇到无效的配置选项时会静默崩溃。就我而言,这是no-trailing-comma必须更改为trailing-comma.

More info here: https://github.com/Microsoft/vscode-tslint/issues/66

更多信息:https: //github.com/Microsoft/vscode-tslint/issues/66

回答by V.B.

On a new machine, I installed VS Code tslint extension before installing tslint itself (via npm), and nothing helped to make it work other than disabling and re-enabling the extension.

在一台新机器上,我在安装 tslint 本身之前安装了 VS Code tslint 扩展(通过 npm),除了禁用和重新启用扩展之外,没有任何帮助使它工作。

enter image description here

在此处输入图片说明

回答by basarat

VS Code doesn't give any warning. What does this extension do

VS Code 没有给出任何警告。这个扩展有什么作用

When in doubt. Restart VSCode.

有疑问时。重新启动 VSCode。

回答by Rjk

In my case it was the .vscode/tasks.jsonfile. I removed and recreated the file and its all working fine now.

就我而言,它是.vscode/tasks.json文件。我删除并重新创建了该文件,现在一切正常。

回答by CertainPerformance

One possibility is that your tslint.json file may not be in proper JSON format. The tslint.json file as shown when opened in VSCode may analyze it for errors using its jsonc parser, which does not show errors when the last key-value pair has a trailing comma (which is invalid in plain JSON). But, the linting process (or at least ms-vscode.vscode-typescript-tslint-plugin) will silently failif the tslint.json is not actual JSON.

一种可能性是您的 tslint.json 文件可能不是正确的 JSON 格式。在 VSCode 中打开时显示的 tslint.json 文件可能会使用其 jsonc parser分析它的错误,当最后一个键值对具有尾随逗号(这在纯 JSON 中无效)时,它不会显示错误。但是,如果 tslint.json 不是实际的 JSON,linting过程(或至少ms-vscode.vscode-typescript-tslint-plugin)将默默地失败

For example, the following will result in a silent failure, without any indication of where the problem is, due to the trailing comma:

例如,由于尾随逗号,以下将导致无提示失败,没有任何指示问题出在哪里:

{
  "extends": "tslint:latest",
  "rules": {
    "ordered-imports": false,
    "prefer-for-of": false,
  }
}

Whereas the following will work as expected:

而以下将按预期工作:

{
  "extends": "tslint:latest",
  "rules": {
    "ordered-imports": false,
    "prefer-for-of": false
  }
}

回答by Andy-Delosdos

Make sure that you've got a valid tslint.json file in your working directory root. There's a good guide here if you scroll down to the readme - https://github.com/palantir/tslint

确保您的工作目录根目录中有一个有效的 tslint.json 文件。如果您向下滚动到自述文件,这里有一个很好的指南 - https://github.com/palantir/tslint

Try making a deliberate error to a TS file, and you should see that the error gets underlined with a squiggly line. I

尝试故意对 TS 文件进行错误处理,您应该会看到错误带有波浪线下划线。一世

Note: I'm using VSCode 1.3.1, and vscode-tslint 0.5.32.

注意:我使用的是 VSCode 1.3.1 和 vscode-tslint 0.5.32。

回答by Animal2

I had the same problem as you. For some reason after updating either TSLint or Visual Studio Code, linting stopped working. After cloning the project Zen recommended in the comments, I received an error saying that TSLint wasn't installed. I installed TSLint globally but not as a dev dependency for my project so after running "npm install tslint --save-dev" Visual Studio Code started linting again.

我和你有同样的问题。出于某种原因,更新 TSLint 或 Visual Studio Code 后,linting 停止工作。克隆Zen在评论中推荐的项目后,收到一个错误,说没有安装TSLint。我全局安装了 TSLint,但不是作为我项目的开发依赖项,因此在运行“npm install tslint --save-dev”后,Visual Studio Code 再次开始 linting。