Typescript: TS6133 'myparam' is declared but its value is never read: Relax for empty function bodies

Created on 6 Aug 2018  ·  3Comments  ·  Source: microsoft/TypeScript

Suggestion

Sometimes developers create a base class that has methods that have an empty implementation and are meant to be (optionally) implemented by child classes. This is sometimes called the "strategy pattern".

Here is an example:

export abstract class WidgetObserver {
    onResized(width: number, height: number): void {}
    onTextChanged(text: string): void {}
    onDestroyed(): void {}
}

The problem is that in TypeScript we will get 3 errors:

TS6133 'width' is declared but its value is never read
TS6133 'height' is declared but its value is never read
TS6133 'text' is declared but its value is never read

Most of the time this error is useful, since it can help reveal bugs that resulted from us forgetting to use a particular parameter, or even help us redesign our code if we realize that a parameter is not actually needed.

But for the case where the function body is empty, this error is not helpful, there is no possibility that the programmer forgot to do something, and it should be clear to the compiler that the programmer knows what they are doing.

In addition to the "strategy pattern" there are a few other patterns of functions with empty bodies.

Summary: I suggest that error TS6133 should be completed suppressed for function parameters if the function body is empty.

Checklist

My suggestion meets these guidelines:

  • [X] This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • [X] This wouldn't change the runtime behavior of existing JavaScript code
  • [X] This could be implemented without emitting different JS based on the types of the expressions
  • [X] This isn't a runtime feature (e.g. new expression-level syntax)
Working as Intended

Most helpful comment

Yes, I am aware of the _ prefix, but for this case it is extremely annoying, since all of the parameters will need to be prefixed, and also the parameter names of the derived implementations won't match up with the parent function signatures

All 3 comments

If it is intentional that you don't want to use the parameter then you can prefix it with _. Naming it _width will get rid of the warning and clearly states to future developers: This is intentionally not used.

https://agentcooper.github.io/typescript-play/?noUnusedParameters=true#code/GYVwdgxgLglg9mABMOcAUB9ARgQwE4BciAzlHjGAOYCURAbnDACaIDeAvgFBA

Yes, I am aware of the _ prefix, but for this case it is extremely annoying, since all of the parameters will need to be prefixed, and also the parameter names of the derived implementations won't match up with the parent function signatures

It's intended that the noUnusedParameters flag simply do what it says it does (warns about unused parameters), rather than turn it into a complex series of guesses about when you wanted or didn't want to ignore a parameter. There are already at least different three ways to suppress this warning.

You can use ts-ignore, or prefix the parameters with _, or use overload signatures (the implementation signature is not visible; this does not create two overloads):

export abstract class WidgetObserver {
    onResized(width: number, height: number): void;
    onResized(): void { }

    onTextChanged(text: string): void;
    onTextChanged(): void {}

    onDestroyed(): void { }
}
Was this page helpful?
0 / 5 - 0 ratings