Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript private property declared but not used

Tags:

typescript

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.

like image 661
tangokhi Avatar asked Nov 16 '25 20:11

tangokhi


1 Answers

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();
}
like image 115
Robby Cornelissen Avatar answered Nov 18 '25 10:11

Robby Cornelissen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!