Tslint 如何禁用错误“ some 浮动被声明但它的值从未被读取”

我使用的是 tlint 得到了错误。

'myVariable' is declared but its value is never read.

我去了记录规则 https://palantir.github.io/tslint/rules/的网站,搜索字符串 is declared but its value is never read,但没有找到那个文本。虽然我可以而且确实在寻找可能与此错误相关的设置,但它不应该是一个猜谜游戏。

抑制/停止此错误所需的配置更改是什么?

同样重要的是 当我在 tslint 中得到一个错误说“这发生了”时,我如何找到使用什么设置来配置或更改 tslint 行为以处理该错误?

我也做了一个网站上的搜索(谷歌搜索我使用的是)

site:palantir.github.io  is declared but its value is never read

但是没有直接命中,所以答案可能在 palantir.github.io 网站上,但是我还没有找到。

其他人如何找到会更改以抑制特定错误的 tslint 变量/配置设置?

请不要建议我注释掉导致问题的代码。我正在寻找一个答案,对于我的更一般的问题,以及具体的问题。谢谢你。

200289 次浏览

第一个问题:

编辑文件: tsconfig.json,添加/修改键 "noUnusedLocals": false

您需要重新启动服务器。

第二个问题:

如果是一个 tslint 错误,则在错误消息中,VS 代码显示应用的 规则

标识符“ doc”从不重新分配; 请使用“ const”而不是“ let”。 (选择-const)

本例中的 prefer-const规则。

_开头的任何参数名称都不受检查限制。请使用 _myVariable而不是 myvariable来删除此警告。

在导致错误的行之前添加这一行:

  /* tslint:disable:no-unused-variable */

您将不再收到 tslint 错误消息。

这是一个比在 tslint.conf 中关闭整个代码库的错误更好的解决方案,因为这样就不会捕获真正没有使用的变量。

我使用 typescript": "2.9.1"tslint": "^5.10.0

我犯了很多错误,比如

Property 'logger' is declared but its value is never read.

另外,我注意到在运行 ng-lint时会收到一个警告

$> ng lint
no-unused-variable is deprecated. Since TypeScript 2.9. Please use the built-in compiler checks instead.

因此,我从 tslint.json中移除了 no-unused-variable规则-这似乎为我解决了问题。

另一种避免这种情况的方法是为每个变量创建 get 方法,如下所示:

get variablename():variabletype{return this.variablename;}

有两种类型的变量,你可以用两种方法来实现

  1. 忽略模式
  2. VarsIgnore 模式

    • no-unused-vars: ["error", { "argsIgnorePattern": "^_" }]
    • no-unused-vars: ["error", { "varsIgnorePattern": "^_" }]

Eslint 中的这两个规则都会忽略任何分别以 _ sign 开头的函数参数和变量

新的解决方案

首先,关闭 tsconfig.json 中的 noUnusedLocals:

{
"compilerOptions": {
"noUnusedLocals": false,
}
}

然后在 .eslintrc.js中修复 eslint 规则:

module.exports = {
rules: {
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': ['error', { 'varsIgnorePattern': '^_', "argsIgnorePattern": "^_" }],
},
};

如果使用 @typescript-eslint/naming-convention规则应该添加 leadingUnderscore: 'allow',例如,如果您使用 Airbnb 配置:

'@typescript-eslint/naming-convention': [
'error',
{
selector: 'variable',
format: ['camelCase', 'PascalCase', 'UPPER_CASE'],
leadingUnderscore: 'allow',
},
{
selector: 'function',
format: ['camelCase', 'PascalCase'],
},
{
selector: 'typeLike',
format: ['PascalCase'],
},
],

注意: 应该手动将 package.json 中的所有相关 eslint包更新到最新版本。

使用 dev.tsconfig.json 扩展 tsconfig.json

并运行命令 tsc-p./dev.tsconfig.json

这将在开发中禁用未使用的变量和未使用的参数

在 dev.tsconfig.json 内部:

{
"extends": "./tsconfig.json",
"compilerOptions": {
"noUnusedLocals": false,
"noUnusedParameters": false,
}
}

我在这个网站上看到了一个解决方案: https://phpenthusiast.com/blog/angular-form-ngform-and-two-ways-data-binding

对我有帮助,但只有50%

这是我修改过的代码:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';// i added this line and one more line.
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';


import { DheerajComponent } from './dheeraj/dheeraj.component'
@NgModule({
declarations: [
AppComponent,
DheerajComponent
],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule, // this one. remaining all default code
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
}

反应中,如果你有这样的东西

export default class Body extends Component {
    

let data = null; // somethings like this line


// ...
// ...

转换成

export default class Body extends Component {
    

data = null; // somethings like this line


// ...
// ...

如果有人需要暂时禁用它,在开发时,最好的方法可能是运行带有额外命令行标志的 tsc编译器:

$npx tsc -w --noUnusedLocals false --noUnusedParameters false