typescript/no-this-alias Correctness
作用
禁止为 this 起别名。
为什么这不好?
将变量赋值为 this,而不是正确地使用箭头函数,可能表明这是 ES2015 之前的写法,或者说明没有很好地管理作用域。
示例
以下是此规则的错误代码示例:
js
const self = this;
setTimeout(function () {
self.doWork();
});以下是此规则的正确代码示例:
js
setTimeout(() => {
this.doWork();
});配置
此规则接受一个包含以下属性的配置对象:
allowDestructuring
type: boolean
default: true
是否允许将 this 解构到局部变量中。
allowedNames
type: string[]
default: []
允许作为 this 别名的变量名数组。
使用方法
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"typescript/no-this-alias": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"typescript/no-this-alias": "error",
},
});bash
oxlint --deny typescript/no-this-alias版本
此规则在 v0.0.7 中添加。
