Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

no-unused-variable: private ctor params shouldn't require this. when used in the ctor #1336

Closed
ChrisMBarr opened this issue Jun 24, 2016 · 3 comments

Comments

@ChrisMBarr
Copy link
Contributor

ChrisMBarr commented Jun 24, 2016

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

class Example {
  constructor(private thing1: string, private thing2: string){
    console.log(thing1);
    console.log(this.thing2);
  }
}

with tslint.json configuration:

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

Actual behavior

The private thing1 variable is reported as unused, even though it is used without the this. prefix, which is allowed in a constructor.

Expected behavior

Used variables in the constructor should not require the this. prefix to be considered "used"

@jkillian
Copy link
Contributor

jkillian commented Jun 24, 2016

this.thing1 vs. thing1 have different meanings - consider the case below:

class Foo {
    constructor(public foo = 20) {
        this.foo = 10;
        foo = 5;
    }
}

const f = new Foo();
alert(f.foo);  // will display 10

If a private parameter property of a constructor is never accessed accessed anywhere in the class as this.myProperty, it actually still is unused as a class member and I think the lint rule is succeeding. Does this line up with what you're seeing from TSLint?

@ChrisMBarr
Copy link
Contributor Author

I was not aware of that, I assumed they meant the same thing. Thanks for the clarification!

@ChrisMBarr
Copy link
Contributor Author

ChrisMBarr commented Jun 24, 2016

Now that I'm thinking about this more... should there be a rule to ban the improper use like this?

constructor(public foo: number) {
    this.foo = 10;
    foo = 5; //<-- violation
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants