class-methods-use-this
Configuration
Rule Details
Enforce that class methods utilize this.
If a class method does not use this, it can sometimes be made into a static function. If you do convert the method into a static function, instances of the class that call that particular method will have to be converted to a static call (i.e., MyClass.callStaticMethod()).
This rule extends the base ESLint class-methods-use-this rule with two TypeScript-specific options: ignoreOverrideMethods (skip members marked with the override modifier) and ignoreClassesThatImplementAnInterface (skip members of classes that implements an interface).
Examples of incorrect code for this rule:
Examples of correct code for this rule:
Options
enforceForClassFields
Type: boolean — Default: true
Enforces that functions used as instance field initializers utilize this.
Examples of incorrect code with { "enforceForClassFields": true } (default):
Examples of correct code with { "enforceForClassFields": false }:
exceptMethods
Type: string[] — Default: []
Allows specified method names to be ignored by this rule. Private class members can be referenced via their #-prefixed name (#foo).
Examples of correct code with { "exceptMethods": ["foo", "#bar"] }:
ignoreOverrideMethods
Type: boolean — Default: false
Whether to ignore class members marked with the override modifier.
Examples of correct code with { "ignoreOverrideMethods": true }:
ignoreClassesThatImplementAnInterface
Type: boolean | 'public-fields' — Default: false
Whether to ignore class members that are defined within a class that implements an interface.
true— ignore every member of any class that implements an interface.'public-fields'— only ignore public members (those without aprivateorprotectedmodifier).
Examples of correct code with { "ignoreClassesThatImplementAnInterface": true }:
Examples of correct code with { "ignoreClassesThatImplementAnInterface": "public-fields" }:
Examples of incorrect code with { "ignoreClassesThatImplementAnInterface": "public-fields" } (private/protected members are still checked):