class-methods-use-this
Enforce that class methods utilize
this.
Some problems reported by this rule are automatically fixable by the --fix ESLint command line option.
This rule extends the base eslint/class-methods-use-this rule.
It adds support for ignoring override methods or methods on classes that implement an interface.
How to Use
module.exports = {
"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"
}
};
Options
See eslint/class-methods-use-this options.
This rule adds the following options:
interface Options extends BaseClassMethodsUseThisOptions {
ignoreOverrideMethods?: boolean;
ignoreClassesThatImplementAnInterface?: boolean;
}
const defaultOptions: Options = {
...baseClassMethodsUseThisOptions,
ignoreOverrideMethods: false,
ignoreClassesThatImplementAnInterface: false,
};
ignoreOverrideMethods
Makes the rule to ignores any class member explicitly marked with override.
Example of a correct code when ignoreOverrideMethods is set to true:
class X {
override method() {}
override property = () => {};
}
Open in PlaygroundignoreClassesThatImplementAnInterface
Makes the rule ignore all class members that are defined within a class that implements a type.
It's important to note that this option does not only apply to members defined in the interface as that would require type information.
Example of a correct code when ignoreClassesThatImplementAnInterface is set to true:
class X implements Y {
method() {}
property = () => {};
}
Open in PlaygroundResources
Taken with ❤️ from ESLint core