Enforce unbound methods are called with their expected scope.
💭
This rule requires type information to run, which comes with performance tradeoffs.
Class method functions don't preserve the class scope when passed as standalone variables ("unbound"). If your function does not access this
, you can annotate it with this: void
, or consider using an arrow function instead. Otherwise, passing class methods around as values can remove type safety by failing to capture this
.
This rule reports when a class method is referenced in an unbound manner.
eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/unbound-method": "error"
}
});
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/unbound-method": "error"
}
};
Try this rule in the playground ↗
Examplesclass MyClass {
public log(): void {
console.log(this);
}
}
const instance = new MyClass();
const myLog = instance.log;
myLog();
const { log } = instance;
const arith = {
double(x: number): number {
return x * 2;
},
};
const { double } = arith;
Open in Playground
class MyClass {
public logUnbound(): void {
console.log(this);
}
public logBound = () => console.log(this);
}
const instance = new MyClass();
const { logBound } = instance;
logBound();
const dotBindLog = instance.logUnbound.bind(instance);
const innerLog = () => instance.logUnbound();
const arith = {
double(this: void, x: number): number {
return x * 2;
},
};
const { double } = arith;
Open in Playground Options
This rule accepts the following options:
type Options = [
{
ignoreStatic?: boolean;
},
];
const defaultOptions: Options = [{ ignoreStatic: false }];
ignoreStatic
Whether to skip checking whether static
methods are correctly bound. Default: false
.
Examples of correct code for this rule with { ignoreStatic: true }
:
class OtherClass {
static log() {
console.log(OtherClass);
}
}
const { log } = OtherClass;
log();
Open in Playground When Not To Use It
If your project dynamically changes this
scopes around in a way TypeScript has difficulties modeling, this rule may not be viable to use. For example, some functions have an additional parameter for specifying the this
context, such as Reflect.apply
, and array methods like Array.prototype.map
. This semantic is not easily expressed by TypeScript. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.
If you're wanting to use toBeCalled
and similar matches in jest
tests, you can disable this rule for your test files in favor of eslint-plugin-jest
's version of this rule.
Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.
See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.
ResourcesRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4