TypeScript Version: 2.2.1
Code
// A *self-contained* demonstration of the problem follows...
abstract class A {
abstract m(test: string);
}
class B extends A {
// error: [ts] 'test' is declared but never used.
m(test: string) {
console.log('a');
}
}
class C extends A {
// all ok
m(test: string) {
console.log(test);
}
}
Expected behavior:
No error shown for the class B.
Actual behavior:
An error is shown in the class B on the test parameter of the method m: [ts] 'test' is declared but never used.
Prefix your parameter with _ to disable the error. this is not specific to abstract methods, all parameters behave this way.
class B extends A {
// error: [ts] 'test' is declared but never used.
m(_test: string) {
console.log('a');
}
}
@mhegazy that's a great workaround, but I don't think it is a good solution :)
If you make the comparison with other typed object oriented languages such as Java and their tooling, you can see this kind of situation is common and handled as in the "expected behaviour" I described.
For example the Eclipse IDE won't show these parameter as unused if you enable the warnings for unused parameters, and Sonarqube, one of the reference in code quality analysis will behave the same.
Conceptually, I don't see any reason to mark them as an error. Optionally we could consider it a warning, but never an error.
@mhegazy that's a great workaround, but I don't think it is a good solution :)
It is not a work around. it is a feature. See docs for more details.
Conceptually, I don't see any reason to mark them as an error. Optionally we could consider it a warning, but never an error.
You can always disable the --noUnusedParamters flag. If you do enable it the flag behavior is rather simple, if you have not used the parameter in the function body, it is marked as "unused".
One thing to note, you are not required in TS to have the same parameters as the function your are implementing, as in Java or C# for instance. So your class can be implemented as:
class B extends A {
m() {
console.log('a');
}
}
It is not a work around. it is a feature. See docs for more details.
it's not a bug, it's feature? ;) j/k
I am asking for a feature improvement then!
I know you can remove the parameters, but what if there is two of them and you only need the second? I guess your answer will be that I can use _.
I understand your position, so feel free to close this issue if this is not the direction that typescript is taking, it seems fair to me :)
Thanks!
Most helpful comment
Prefix your parameter with
_to disable the error. this is not specific to abstract methods, all parameters behave this way.