ChatGPT解决这个技术问题 Extra ChatGPT

tslint how to disable error "someVariable is declared but its value is never read"

I'm using tslint, and got the error.

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

I went to the website that documents the rules https://palantir.github.io/tslint/rules/ and searched for the string is declared but its value is never read but didn't find that text. While I can and did look for settings that might be tied to this error, it shouldn't be a guessing game.

What is the configuration change needed to suppress/stop this error?

Just as importantly, when I get an error in tslint that says "this happened" how can I find what setting is used to configure or change the tslint behavior on how to handle that error?

I also did a search on the website (google search I used was)

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

but a direct hit did not appear, so the answer might be on the palantir.github.io website but I just didn't (yet) find it.

How do others find the tslint variable/configuration settings that change to suppress a particular error?

Please refrain from suggesting I comment out the code that is causing the problem. I'm looking for an answer to my more general question as well as to the specific question. Thank you.

Have you tried setting noUnusedLocals to false in your compilerOptions ? Recommended by this post: github.com/Microsoft/TypeScript/issues/…
"noUnusedLocals" : false, + "noUnusedParameters": false, worked for me
This rule is like having to drive at 20mph with a Ferrari
as if you would like to have a ferrari with a bunch of loose / unused pieces in the engine :)

J
Jaroslav Bezděk

Any parameter name starting with _ is exempt from the check. Use _myVariable instead of myvariable to remove this warning.


It does not happen automatically, you have to specify ignore pattern. "no-unused-variable": [true, {"ignore-pattern": "^_"}] But yes, this is a good solution.
I didn't have to specify that pattern, worked for me right away
As an update, "no-unused-variable" is deprecated since TypeScript 2.9 github.com/palantir/tslint/issues/4046
Worth noting that since tslint is deprecated, you should use eslint and typescript-eslint instead. Using _ does not automatically bypass that rule no-unused-vars, but requires configuration like Rachit said. eslint.org/docs/rules/no-unused-vars. Using _ still does bypass the TS compiler automatically.
The current preferred way to implement this (to the best of my knowledge) is, in your .eslintrc.js file: ` '@typescript-eslint/no-unused-vars': ['error', { 'argsIgnorePattern': '^_' }],` use argsIgnorePattern if referring to arguments in something like a decorator, and/or varsIgnorePattern to ignore declared variables.
L
Liam

Fist question:

Edit the file:tsconfig.json, adding/modifying key "noUnusedLocals": false.

You'll need to restart the server.

Second question:

If it is a tslint error; VS Code shows, in the error message, the rule that's been applied.

Identifier 'doc' is never reassigned; use 'const' instead of 'let'. (prefer-const)

The prefer-const rule in this case.


Or you can apparently declare variables you only use in the template protected rather than turning off checking for any unused locals.
e
edwin

Add this line just before the line which causes the error:

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

You will no longer receive the tslint error message.

This is a better solution than turning off the error for you whole codebase in tslint.conf because then it wouldn't catch variables that really aren't used.


M
Masih Jahangiri

New solution

First, turn off noUnusedLocals in tsconfig.json:

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

Then fix eslint rules in .eslintrc.js:

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

And If using @typescript-eslint/naming-convention rule should add leadingUnderscore: 'allow', For example, if you are using 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'],
  },
],

Note: you should update all related eslint packages in package.json to the latest version manually.


P
PatS

Extend the tsconfig.json with dev.tsconfig.json

And run the command tsc -p ./dev.tsconfig.json

This will disable the unused variable and unused parameter in development

Inside dev.tsconfig.json:

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

Thanks it was the noUnusedParameters that was screwing me over
W
Wand Maker

I am using typescript": "2.9.1" with tslint": "^5.10.0.

I was getting tons of error such as

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

Also, I observed that I was getting a warning when running ng-lint

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

So, I removed the no-unused-variable rule fromt tslint.json - and that seems to solve the problem for me.


K
Kalana

There are two type of variables and you can do it in two ways

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

Both these rule in eslint will ignore any function arguments and variables that starts with _ sign respectively


I
IndieGameDev

I saw a solution on this web site: https://phpenthusiast.com/blog/angular-form-ngform-and-two-ways-data-binding.

it helped me but only 50% of it

This is my modified code:

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 { 
}

N
Nima

In react if you have some thing like this

export default class Body extends Component {
    
    let data = null; // somethings like this line

    // ...
    // ...

convert it to

export default class Body extends Component {
    
    data = null; // somethings like this line

    // ...
    // ...

Just remove let or const or var
D
Dmitry Koroliov

If someone needs to disable it just for a moment, when developing, the best way may be to just run the tsc compiler with an additional command line flag:

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

J
Jorgé Reyniers

Another way to avoid this is to create a get-method for every variable you have, like this:

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

This adds potentially unnecessary code to fix a linter warning. Much better to actually configure the linter to your liking than to add unused code.
This is no unnecessary code if you work with classes with private properties.