@typescript-eslint/unbound-method
Disallow taking a method off its object without binding it
Also called typescript-eslint/unbound-method, typescript/unbound-method
A method gets its this from how it's called. If you store user.greet in a variable or pass it as a callback, it's later called without the object, so this is undefined and the first this.name throws.
Calling the method, comparing it, testing it in a condition, and typeof are fine. So are properties that hold an arrow function, methods declared with this: void, and built-in functions that don't use this, such as console.log, Math.max, and JSON.parse.
bun lint --type-aware turns this rule on in TypeScript files.
Configure#
Off by default. This rule needs types, so turning it on makes bun lint type check your code first.
[lint.rules]
"@typescript-eslint/unbound-method" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"@typescript-eslint/unbound-method" = ["error", {
ignoreStatic = false,
}]Examples#
class User {
name = "Ada";
greet() {
return `Hello ${this.name}`;
}
}
const user = new User();
const greet = user.greetA method that is not declared with this: void may cause unintentional scoping of this when separated from its object.Consider using an arrow function or explicitly .bind()ing the method to avoid calling the method with an unintended this value. If a function does not access this, it can be annotated with this: void.;
const { greetA method that is not declared with this: void may cause unintentional scoping of this when separated from its object.Consider using an arrow function or explicitly .bind()ing the method to avoid calling the method with an unintended this value. If a function does not access this, it can be annotated with this: void.: hello } = user;
setTimeout(user.greetA method that is not declared with this: void may cause unintentional scoping of this when separated from its object.Consider using an arrow function or explicitly .bind()ing the method to avoid calling the method with an unintended this value. If a function does not access this, it can be annotated with this: void., 100);class User {
name = "Ada";
greet() {
return `Hello ${this.name}`;
}
wave = () => `${this.name} waves`;
static create(this: void) {
return new User();
}
}
const user = new User();
const greet = user.greet.bind(user);
setTimeout(() => user.greet(), 100);
setTimeout(user.wave, 100);
const create = User.create;
const log = console.log;Options#
| Option | Default | What it does |
|---|---|---|
ignoreStatic | false | Don't check static methods. |
Id.next doesn't use this, but it's reported unless ignoreStatic is on:
class Id {
static next() {
return crypto.randomUUID();
}
}
const ids = [1, 2, 3].map(Id.nextA method that is not declared with this: void may cause unintentional scoping of this when separated from its object.Consider using an arrow function or explicitly .bind()ing the method to avoid calling the method with an unintended this value. If a function does not access this, it can be annotated with this: void.);