Skip to content

[unbound-method] referencing a property with a function type should cause error #1869

@markfields

Description

@markfields

Repro

{
  "rules": {
    "@typescript-eslint/unbound-method": [
            "error",
            {
                "ignoreStatic": true
            }
        ]
  }
}
class SomeClass {
    functionProperty = function(this: any) { console.log(`this is ${this}`); };
}

const myFunctionProperty = new SomeClass().functionProperty;  // X - Missing eslint error
myFunctionProperty();  // Doesn't work, despite no eslint error

Expected Result
unbound-method error when initializing myfunctionProperty

Actual Result
No error, but calling myFunctionProperty() prints out this is undefined

Additional Info
I wonder if there should also be a rule for requiring the this parameter in inline function definitions be declared to have the most specific type possible(?). If I declare functionProperty = function(this: SomeClass) { ... } then there's a compiler error when I try to call myFunctionProperty():

The 'this' context of type 'void' is not assignable to method's 'this' of type 'MyClass'.ts(2684)

Versions

package version
@typescript-eslint/eslint-plugin 2.17.0
@typescript-eslint/parser 2.17.0
TypeScript 3.7.5
ESLint 6.8.0
node 10.19.0
npm 6.13.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions