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
调用。
¥If you're working with jest
, you can use eslint-plugin-jest
's version of this rule to lint your test files, which knows when it's ok to pass an unbound method to expect
calls.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/unbound-method": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/unbound-method": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ Incorrect
- ✅ Correct
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.logUnbound.bind(instance);
const innerLog = () => instance.logUnbound();
// 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选项
该规则接受以下选项:
type Options = [
{
/** Whether to skip checking whether `static` methods are correctly bound. */
ignoreStatic?: boolean;
},
];
const defaultOptions: Options = [{ ignoreStatic: false }];
¥Options
ignoreStatic
Whether to skip checking whether static
methods are correctly bound. Default: false
.
带有 { 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何时不使用它
¥When Not To Use It
如果你的项目以 TypeScript 难以建模的方式动态更改 this
范围,则此规则可能无法使用。例如,某些函数具有用于指定 this
上下文的附加参数,例如 Reflect.apply
,以及像 Array.prototype.map
这样的数组方法。TypeScript 不易表达这种语义。你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。
¥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.
如果你想在 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 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.
'## 资源'