I've encountered this a bunch and have finally decided to figure out what the right way to do it is. If I have an abstract parent class that declares a method, and then some of the concrete sub-classes implement real logic in their implementation (and, obviously, use the method parameters), but some of the sub-classes don't need to do anything in that method (so don't use the method parameters) - the ones that don't have to do anything generate spurious (in my view) warnings in my code.
For instance, if I have some classes that look like:
abstract class Child {
constructor(protected name: string) {
connsole.log(name + " was just born.");
}
abstract tellThemToDoSomething(command: string);
}
class GoodChild extends Child {
constructor(name: string) { super(name); }
tellThemToDoSomething(command: string) {
console.log("I will do " + command);
}
}
class BadChild extends Child {
constructor(name: string) { super(name); }
tellThemToDoSomething(command: string) {
// bad children just ignore what their parents tell them
}
}
Then I get a (TSLint? JSLint? WebStorm?) warning in WebStorm for the unused parameter in the BadChild
tellThemToDoSomethign()
method.
I know of a few options, but none seem great.
1) Ignore it (but that seems suboptimal, since I'll grow blind to real warnings)
2) Remove the parameter from the BadChild's method implementation (but that unfortunately removes useful information, since I may later want to implement it, and not having the signature there makes it more difficult, and I also get errors from callers expecting there to be parameters - though given that I should be able to call a method with extraneous parameters in JavaScript, I be there's a way around that problem)
3) Tell WebStorm(TSLint) to stop warning on unused parameters (but that seems suboptimal since I won't get warnings in other real problem situations)
4) Do something meaningless with the parameter so it is not unused (obviously not great)
What is do experienced Java/TypeScript coders do in situations like this? Is there an easy way to tell WebStorm/TSLint to ignore the parameter in individual cases like this? Or, even better, a way to tell it to ignore unused parameters in sub-class implementations of abstract methods, as long as some implementations actually use the parameters?
I'm a little unsure on where the warning is coming from, though, since my brief googling shows a TSLint warning for unused-variables but not unused-parameters, and adding a // tslint:ignore-next-line:no-unused-parameter
(or no-unused-variable
) didn't make the warning go away. So I guess it's possible the warning is coming from WebStorm itself? Further, the warning is "Unused parameter Foo: Checks JavaScript parameter, local variable, function, classes and private member declarations to be used in given file scope". So it doesn't look like a TSLint warning. And if I look in my WebStorm Preferences -> Languages & Frameworks -> Javascript -> Code Quality Tools
, none of the linters are enabled (JSLint, JSHint, ESLint, etc.). So any idea where the error is coming from?
I've not coded in TypeScript long enough to have a feel for what the ideal dial on this warning should be.
/* tslint:disable:no-unused-variable */
– KavindraBadChild
and reenable it just after the class using/* tslint:enable:no-unused-variable */
– Kavindra