Rule: no-unbound-method

Warns when a method is used outside of a method call.

Rationale

Class functions don’t preserve the class scope when passed as standalone variables. For example, this code will log the global scope (window/global), not the class instance:

class MyClass {
    public log(): void {
        console.log(this);
    }
}

const instance = new MyClass();
const log = instance.log;

log();

You need to either use an arrow lambda (() => {...}) or call the function with the correct scope.

class MyClass {
    public logArrowBound = (): void => {
        console.log(bound);
    };

    public logManualBind(): void {
        console.log(this);
    }
}

const instance = new MyClass();
const logArrowBound = instance.logArrowBound;
const logManualBind = instance.logManualBind.bind(instance);

logArrowBound();
logManualBind();
Notes:

Config

You may optionally pass “ignore-static” to ignore static methods.

Config examples
"no-unbound-method": true
"no-unbound-method": [true, "ignore-static"]
Schema
{
  "type": "string",
  "enum": [
    "ignore-static"
  ]
}