【问题标题】:tslint how to disable error "someVariable is declared but its value is never read"tslint 如何禁用错误“someVariable 已声明但其值从未被读取”
【发布时间】:2018-10-05 06:59:27
【问题描述】:

我正在使用 tslint,但出现错误。

'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 变量/配置设置?

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

【问题讨论】:

  • 您是否尝试在您的compilerOptions 中将noUnusedLocals 设置为false?本帖推荐:github.com/Microsoft/TypeScript/issues/…
  • "noUnusedLocals" : false, + "noUnusedParameters": false,为我工作
  • 这条规则就像开法拉利必须以 20 英里/小时的速度行驶
  • 就好像你想要一辆法拉利,引擎中有一堆松散/未使用的零件:)

标签: tslint


【解决方案1】:

拳头问题:

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

您需要重新启动服务器。

第二个问题:

如果是 tslint 错误; VS Code 在错误消息中显示已应用的规则

永远不会重新分配标识符“doc”;使用“常量”而不是“让”。 (首选常量)

本例中的prefer-const 规则。

【讨论】:

【解决方案2】:

任何以 _ 开头的参数名称都免于检查。使用_myVariable 而不是myvariable 来删除此警告。

【讨论】:

  • 它不会自动发生,您必须指定忽略模式。 "no-unused-variable": [true, {"ignore-pattern": "^_"}] 但是是的,这是一个很好的解决方案。
  • 我不必指定该模式,立即为我工作
  • 作为更新,"no-unused-variable" 自 TypeScript 2.9 起已弃用 github.com/palantir/tslint/issues/4046
  • 值得注意的是,由于 tslint 已被弃用,您应该改用 eslint 和 typescript-eslint。使用 _ 不会自动绕过该规则 no-unused-vars,但需要像 Rachit 所说的那样进行配置。 eslint.org/docs/rules/no-unused-vars。使用 _ 仍然会自动绕过 TS 编译器。
  • 当前首选的实现方式(据我所知)是,在您的.eslintrc.js 文件中:` '@typescript-eslint/no-unused-vars': ['error' , { 'argsIgnorePattern': '^_' }],` 使用 argsIgnorePattern 如果引用装饰器之类的参数,和/或 varsIgnorePattern 忽略声明的变量。
【解决方案3】:

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

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

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

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

【讨论】:

    【解决方案4】:

    我正在使用 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 规则 - 这似乎解决了我的问题。

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

      • 这会添加可能不必要的代码来修复 linter 警告。根据自己的喜好实际配置 linter 比添加未使用的代码要好得多。
      • 如果您使用具有私有属性的类,这不是不必要的代码。
      【解决方案6】:

      有两种类型的变量,您可以通过两种方式进行操作

      1. argsIgnorePattern
      2. varsIgnorePattern

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

      eslint 中的这两条规则都会分别忽略任何以 _ 符号开头的函数参数和变量

      【讨论】:

        【解决方案7】:

        新解决方案

        首先,关闭tsconfig.json中的noUnusedLocals

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

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

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

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

        '@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 包更新到最新版本。

        【讨论】:

          【解决方案8】:

          用 dev.tsconfig.json 扩展 tsconfig.json

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

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

          dev.tsconfig.json 内部:

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

          【讨论】:

          • 感谢 noUnusedParameters 把我搞砸了
          【解决方案9】:

          我在这个网站上看到了一个解决方案: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 { 
          }
          

          【讨论】:

            【解决方案10】:

            如果你有这样的事情,请在 react

            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
            
                // ...
                // ...
            

            【讨论】:

            • 只需删除 letconstvar
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-08-10
            • 2018-09-28
            • 1970-01-01
            • 2020-01-08
            • 1970-01-01
            • 1970-01-01
            • 2021-11-08
            相关资源
            最近更新 更多