tangokhi
tangokhi

Reputation: 1015

Typescript private property declared but not used

I have a private property declared in the constructor and also used in the constructor to retrieve some value. I get TS6138: PROPERTY 'xxxx' is declared but never used.

constructor(private xxxx: Ixxxx) {
    this.abc = xxxx.get();
}

I am upgrading to typescript 2.4.2. If I remove private then the error goes away. Obviously the property becomes public which I don't want.

Upvotes: 3

Views: 1342

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97140

The warning is correct, you're referencing the constructor argument, not the property. If you want to access the property, you'd have to:

constructor(private xxxx: Ixxxx) { // xxxx is constructor arg and private property
    this.abc = this.xxxx.get();
}

If you're not planning on using the property anywhere else in your class, you might as well remove the private modifier and use the constructor argument instead:

constructor(xxxx: Ixxxx) { // xxxx is constructor arg
    this.abc = xxxx.get();
}

Doing this will not result in xxxx becoming a public property. Only adding the public keyword will do that:

constructor(public xxxx: Ixxxx) { // xxxx is constructor arg and public property
    this.abc = this.xxxx.get();
}

Upvotes: 4

Related Questions