Typescript:1172 should not apply to "this: void"

Sonar version: Version 9.4 (build 54424)

TypeScript has a special syntax to declare the type of this in a function by using it as the first parameter in the function declaration (reference).
In particular, this: void can be used to prevent calls to this in a function:

function doSomething(this: void) {
    this.someProperty // will not compile
}

Code like the above will result in the following message (typescript:1172):

Remove the unused function parameter “this” or rename it to “_this” to make intention explicit.

This should not happen because:

  • by using this: void it is made clear that this will never be used
  • “this” is not a even a valid name for a function parameter in JavaScript and will cause a compilation error, so even in cases where this is not declared as void the message is misleading.

Overall I think the this parameter in TypeScript warrants special treatment.

2 Likes

Hello @m-gallesio,

Thank you for your feedback.

Indeed, as you rightfully pointed out, the rule should not apply to parameters named this.

I created this ticket to address this false positive as soon as possible.

Regards,
Yassin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.