Tslint: no-unused-variable: shouldn't warn about all params in some cases

Created on 24 Jun 2016  路  8Comments  路  Source: palantir/tslint

Bug Report

  • TSLint version: 3.11.0
  • TypeScript version: 1.8.10
  • Running TSLint via: gulp-tslint via Visual Studio 2015

    TypeScript code being linted

this.$scope.$on("$myEvent", (event: angular.IAngularEvent, customerNumber: string) => {
    this.getCustomerInfo(customerNumber);
});

with tslint.json configuration:

"no-unused-variable": [ true, "check-parameters" ],

Actual behavior

The event parameter is reported as unused, which _is technically correct_... but it has to be there so I can access the 2nd parameter, which I need.

Right now I'm needing to put comments like // tslint:disable-next-line:no-unused-variable all over the place.

Expected behavior

Since I only need to use the 2nd parameter, it seems dumb to warn me about the first parameter not being used since this is how the framework I'm using works.

Most helpful comment

to me it is a bug, tslint should not throw error in this case.

All 8 comments

You can use the "ignore-pattern" option in this case to specify a regex that corresponds to variables that are intentionally unused. For example,

"no-unused-variable": [true, {"ignore-pattern": "^_"}]

would ignore all variables that start with an underscore and not report errors when they are unused.

Going to close this issue because "ignore-pattern" is designed exactly for this scenario!

so I'd have to go through my code and rename variables based on their parameter order just to satisfy this rule?

Yep, but it's not that unusual: starting unused variables with an underscore is a standard convention in languages with stricter compilers, like Go and Rust, as far as I know.

So if I were to rename that first var to _event and later on it was used by someone who was not aware of this convention, would that be flagged?

Good question - I believe the rule only works one way here. TSLint won't complain if you use a variable that meets the "ignore-pattern" option

Perhaps that could be a configurable option to add to this rule?

to me it is a bug, tslint should not throw error in this case.

@marcoturi no-unused-variable is deprecated in favor of compiler options and we plan to remove it in the next major version, see #1481. I filed #1851 to address the sparse documentation around this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

avanderhoorn picture avanderhoorn  路  3Comments

dashmug picture dashmug  路  3Comments

ghost picture ghost  路  3Comments

ghost picture ghost  路  3Comments

jacob-robertson picture jacob-robertson  路  3Comments