禁止在this值为undefined的上下文中使用this。
规则配置
// code-linter.json5
{
"rules": {
"@typescript-eslint/no-invalid-this": "error"
}
} 正例
// ts代码文件中需要添加"use strict"
function baz(arg0: () => object) {
return arg0;
}
export class Bar {
public a: number;
public constructor() {
this.a = 0;
baz(() => this);
}
} 反例
// ts代码文件中需要添加"use strict"
function baz(arg0: () => object) {
return arg0;
}
export function foo1() {
this.a = 0;
baz(() => this);
}
export const foo2 = () => {
this.a = 0;
baz(() => this);
};