Skip to main content

class-methods-use-this

强制类方法使用 this.

🧱

This is an "extension" rule that replaces a core ESLint rule to work with TypeScript. See Rules > Extension Rules.

This rule extends the base class-methods-use-this rule from ESLint core. 它增加了对忽略 override 方法和/或实现接口的类上的方法的支持。它还支持自动访问器属性。

¥It adds support for ignoring override methods and/or methods on classes that implement an interface. It also supports auto-accessor properties.

如何使用

eslint.config.mjs
export default tseslint.config({
rules: {
// Note: you must disable the base rule as it can report incorrect errors
"class-methods-use-this": "off",
"@typescript-eslint/class-methods-use-this": "error"
}
});

在线运行试试这个规则 ↗

选项

See eslint/class-methods-use-this's options.

¥Options

该规则添加了以下选项:

¥This rule adds the following options:

interface Options extends BaseClassMethodsUseThisOptions {
ignoreOverrideMethods?: boolean;
ignoreClassesThatImplementAnInterface?: boolean | 'public-fields';
}

const defaultOptions: Options = {
...baseClassMethodsUseThisOptions,
ignoreOverrideMethods: false,
ignoreClassesThatImplementAnInterface: false,
};

ignoreOverrideMethods

是否忽略标有 override 修饰符的成员。 Default: false.

ignoreOverrideMethods 设置为 true 时的正确代码示例:

¥Example of correct code when ignoreOverrideMethods is set to true:

abstract class Base {
abstract method(): void;
abstract property: () => void;
}

class Derived extends Base {
override method() {}
override property = () => {};
}
Open in Playground

ignoreClassesThatImplementAnInterface

是否忽略在 implements 类型的类中定义的类成员。 Default: false.

如果指定,它可以是:

¥If specified, it can be either:

  • true:忽略所有实现接口的类

    ¥true: Ignore all classes that implement an interface

  • 'public-fields':仅忽略实现接口的类的公共字段

    ¥'public-fields': Ignore only the public fields of classes that implement an interface

请注意,此选项适用于所有类成员,而不仅仅是接口中定义的成员。

¥Note that this option applies to all class members, not just those defined in the interface.

true

ignoreClassesThatImplementAnInterface 设置为 true 时的代码示例:

¥Examples of code when ignoreClassesThatImplementAnInterface is set to true:

class Standalone {
method() {}
property = () => {};
}
Open in Playground

'public-fields'

ignoreClassesThatImplementAnInterface 设置为 'public-fields' 时的错误代码示例:

¥Example of incorrect code when ignoreClassesThatImplementAnInterface is set to 'public-fields':

interface Base {
method(): void;
}

class Derived implements Base {
method() {}
property = () => {};

private privateMethod() {}
private privateProperty = () => {};

protected protectedMethod() {}
protected protectedProperty = () => {};
}
Open in Playground

何时不使用它

¥When Not To Use It

如果你的项目以 TypeScript 难以建模的方式动态更改 this 范围,则此规则可能无法使用。你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。

¥If your project dynamically changes this scopes around in a way TypeScript has difficulties modeling, this rule may not be viable to use. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.

资源

Taken with ❤️ from ESLint core.