unbound-method
Enforce unbound methods are called with their expected scope.
该规则需要 类型信息 才能运行。
类方法函数作为独立变量传递时不保留类作用域 ("unbound")。
如果你的函数不访问 this
、你可以用 this: void
注释它,或者考虑使用箭头函数代替。
否则,将类方法作为值传递可能会因无法捕获 this
而消除类型安全性。
英: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.
如果你使用 jest
,则可以使用 此规则的 eslint-plugin-jest
版本 来检查你的测试文件,它知道何时可以将未绑定的方法传递给 expect
调用。
module.exports = {
"rules": {
"@typescript-eslint/unbound-method": "error"
}
};
示例
- ❌ 不正确
- ✅ 正确
class MyClass {
public log(): void {
console.log(this);
}
}
const instance = new MyClass();
// This logs the global scope (`window`/`global`), not the class instance
const myLog = instance.log;
myLog();
// This log might later be called with an incorrect scope
const { log } = instance;
// arith.double may refer to `this` internally
const arith = {
double(x: number): number {
return x * 2;
},
};
const { double } = arith;
Open in Playgroundclass MyClass {
public logUnbound(): void {
console.log(this);
}
public logBound = () => console.log(this);
}
const instance = new MyClass();
// logBound will always be bound with the correct scope
const { logBound } = instance;
logBound();
// .bind and lambdas will also add a correct scope
const dotBindLog = instance.logBound.bind(instance);
const innerLog = () => instance.logBound();
// arith.double explicitly declares that it does not refer to `this` internally
const arith = {
double(this: void, x: number): number {
return x * 2;
},
};
const { double } = arith;
Open in Playground选项
ignoreStatic
此规则的 correct 代码与 { ignoreStatic: true }
的示例:
英:Examples of correct code for this rule with { ignoreStatic: true }
:
class OtherClass {
static log() {
console.log(OtherClass);
}
}
// With `ignoreStatic`, statics are assumed to not rely on a particular scope
const { log } = OtherClass;
log();
Open in Playground何时不使用它
如果你的项目以 TypeScript 建模困难的方式动态更改 this
范围,则此规则可能不可行。
一种可能的困难模式是,如果你的代码在使用后有意等待绑定方法,例如随方法一起传递 scope: this
。
你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。
英:If your project dynamically changes this
scopes around in a way TypeScript has difficulties modeling, this rule may not be viable to use.
One likely difficult pattern is if your code intentionally waits to bind methods after use, such as by passing a scope: this
along with the method.
You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.
如果你想在 jest
测试中使用 toBeCalled
和类似的匹配,你可以为你的测试文件禁用此规则,以支持 此规则的 eslint-plugin-jest
版本。
英: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 Options = [
{
/** Whether to skip checking whether `static` methods are correctly bound. */
ignoreStatic?: boolean;
},
];
const defaultOptions: Options = [{ ignoreStatic: false }];