Tslint: "prefer-function-over-method" should allow methods that are required to satisfy an interface

Created on 8 Dec 2017  ยท  8Comments  ยท  Source: palantir/tslint

Bug Report

  • __TSLint version__: 5.7.0
  • __TypeScript version__: 2.4.2
  • __Running TSLint via__: CLI

TypeScript code being linted

interface SomeInterface {
  someMethod: () => void;
}

class Impl implements SomeInterface {
  someMethod(): void {
    return;
  }
}

with tslint.json configuration:

{
    "rules": {
        "prefer-function-over-method": true
    }
}

Actual behavior

ERROR: test.ts[6, 3]: Class method does not use 'this'. Use a function instead.

Expected behavior

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.

Hard Requires Type Checker Rule Enhancement ๐ŸŒน R.I.P. ๐ŸŒน

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-override config option.

All 8 comments

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! ๐Ÿ‘‹

Was this page helpful?
0 / 5 - 0 ratings