I've come across this scenario multiple times and have finally decided to explore the correct approach for handling it. When dealing with an abstract parent class that declares a method, and some concrete sub-classes implementing actual logic in their implementation while others do not need to utilize the method parameters leading to what I consider unnecessary warnings in my code.
For example, consider the following classes:
abstract class Child {
constructor(protected name: string) {
console.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
}
}
In this setup, I receive warnings, possibly from TSLint or WebStorm, for the unused parameter within the BadChild
's tellThemToDoSomething()
method.
I am aware of a few potential solutions, but none seem optimal.
1) Ignore the warning (although this may cause me to overlook legitimate warnings in the future).
2) Remove the parameter from the BadChild's method implementation (which eliminates useful information for possible future implementations and might lead to errors from callers expecting parameters).
3) Instruct WebStorm (or TSLint) to suppress warnings about unused parameters (yet this may hide genuine issues).
4) Perform a meaningless action with the parameter to prevent it from being considered unused (not an ideal solution).
What is the recommended practice among experienced Java/TypeScript developers in situations like this? Is there a straightforward way to instruct WebStorm/TSLint to disregard the parameter only in specific cases like this? Alternatively, is there a method to instruct them to overlook unused parameters in subclass implementations of abstract methods as long as some implementations actually utilize the parameters?
I am slightly unsure of the source of the warning; a quick search indicates a TSLint warning for unused variables, but attempts to suppress it using "// tslint:ignore-next-line:no-unused-parameter" did not resolve the issue. This leads me to believe that the warning may stem from WebStorm itself. Upon inspecting WebStorm's preferences under JavaScript Code Quality Tools, none of the linters are enabled (JSLint, JSHint, ESLint, etc.). Where could this error be originating from?
Given my limited experience with TypeScript, I'm uncertain about the appropriate level for this particular warning.