interface SomeInterface {
someMethod: () => void;
}
class Impl implements SomeInterface {
someMethod(): void {
return;
}
}
with tslint.json configuration:
{
"rules": {
"prefer-function-over-method": true
}
}
ERROR: test.ts[6, 3]: Class method does not use 'this'. Use a function instead.
TSLint should pass, because extracting this method into a function would cause the class to no longer satisfy the interface.
I know you can add the "allow-public" config, which would allow this to pass, but I want to catch other public methods that aren't required by interfaces.
Will this pass?
interface ISomeInterface {
doSomething: () => void;
}
export class SomeClass implements ISomeInterface {
public doSomething: () => void = foo;
}
export function foo(): void {
// ...stuff
return;
}
Doesn't seem too bad. And the rule in question is for those who want to use the function syntax _whenever_ possible after all.
I have run into this as well. I think the workaround in the previous comment is clever, but my preference for overrides (both public and protected) is to keep them in the class. I think the aliasing technique makes the code a little harder to read. I use the rule to detect helper methods that should be functions, not "all methods that could be functions".
So, my preference would be to make the change as requested by @treythomas123. If there are people who prefer the aliasing technique, then an acceptable plan B would be to add an allow-override config option.
allow-override is a great idea
@ajafff I'm interested in your opinion here. You wrote this line in preferFunctionOverMethodRule.ts:
~ts
// TODO: handle the override keyword once it lands in the language
~
Is it necessary to wait for the override keyword - is it possible to detect a matching signature in a parent class or interface? If so, should such methods always be exempt, or should a config option be necessary?
Any update on this? My current work around is to use allow-public, but I would rather this.
@rope-hmg no update other than #4534. This issue will be closed soon if no PR is sent to address it. If you have the time, that'd be swell!
โ ๏ธ TSLint's time has come! โ ๏ธ
TSLint is no longer accepting most feature requests per #4534. See typescript-eslint.io for the new, shiny way to lint your TypeScript code with ESLint. โจ
It was a pleasure open sourcing with you all!
๐ค Beep boop! ๐ TSLint is deprecated ๐ _(#4534)_ and you should switch to typescript-eslint! ๐ค
๐ This issue is being locked to prevent further unnecessary discussions. Thank you! ๐
Most helpful comment
I have run into this as well. I think the workaround in the previous comment is clever, but my preference for overrides (both public and protected) is to keep them in the class. I think the aliasing technique makes the code a little harder to read. I use the rule to detect helper methods that should be functions, not "all methods that could be functions".
So, my preference would be to make the change as requested by @treythomas123. If there are people who prefer the aliasing technique, then an acceptable plan B would be to add an
allow-overrideconfig option.