typescript/no-this-alias 正确性
它的作用
禁止对 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 的变量名称数组。
如何使用
通过配置文件或命令行工具启用此规则,可以使用:
json
{
"rules": {
"typescript/no-this-alias": "error"
}
}bash
oxlint --deny typescript/no-this-alias