no-this-alias
禁止使用
this
别名.
✅
在 ESLint 配置 中扩展"plugin:@typescript-eslint/recommended"
可启用此规则。
将变量分配给 this
而不是正确使用箭头 lambda 可能是 ES6 之前的实践或范围管理不善的症状。
¥Assigning a variable to this
instead of properly using arrow lambdas may be a symptom of pre-ES6 practices
or not managing scope well.
- 扁平配置
- 旧版配置
eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-this-alias": "error"
}
});
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-this-alias": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ 错误
- ✅ 正确
const self = this;
setTimeout(function () {
self.doWork();
});
Open in PlaygroundsetTimeout(() => {
this.doWork();
});
Open in Playground