Tslint: Constructor params used without the "this." are marked as unused.

Created on 23 Aug 2017  路  2Comments  路  Source: palantir/tslint

Bug Report

  • __TSLint version__: 5.6.0
  • __TypeScript version__: 2.3.4
  • __Running TSLint via__: ionic scripts

TypeScript code being linted

constructor(
    private modalCtrl: ModalController,
    private navParams: NavParams,
    private changeDetectorRef: ChangeDetectorRef
  ) {
    this.saloon = navParams.data.saloon
    this.services = navParams.data.services
  }

with tslint.json configuration:

{
  "rules": {
    "no-duplicate-variable": true,
    "no-unused-variable": [
      true
    ]
  },
  "rulesDirectory": [
    "node_modules/tslint-eslint-rules/dist/rules"
  ]
}

Actual behavior

Property 'navParams' is declared but never used.

  L20:  private modalCtrl: ModalController,
  L21:  private navParams: NavParams,
  L22:  private saloonService: SaloonService,

Expected behavior

No warnings about the 'navParams' variable that is actually been used.

All 2 comments

These are no regular constructor parameters. They're so called parameter properties: https://www.stevefenton.co.uk/2013/04/stop-manually-assigning-typescript-constructor-parameters/

TypeScript automatically creates a property of the same name and type as the parameter. That property is never used. If you don't need the property, just omit the access modifier (private in your case) and everything is fine.

So this is working as intended.

You might argue that you are using the properties in the template, because it seems that's an Angular component. But that's not a valid use of private properties, refer to #3094 for an explanation.

I thought, it's, a bug, but actually not. Here is solution in duplicate issue:
https://github.com/palantir/tslint/issues/3420

Was this page helpful?
0 / 5 - 0 ratings