Skip to content
← Back to rules

eslint/class-methods-use-this Restriction

What it does ​

Enforce that class methods utilize this.

Why is this bad? ​

For class methods that do not use this, you should consider converting them to static methods. This is not always possible or desirable, but it can help clarify that the method does not rely on instance state.

If you do convert the method into a static function, instances of the class that call that particular method have to be converted to a static call as well.

Examples ​

Examples of incorrect code for this rule:

js
class A {
  foo() {
    console.log("Hello World");
  }
}

Examples of correct code for this rule:

js
class A {
  foo() {
    this.bar = "Hello World"; // OK, this is used
  }
}

class B {
  constructor() {
    // OK. constructor is exempt
  }
}

class C {
  static foo() {
    // OK. static methods aren't expected to use this.
  }
}

Configuration ​

This rule accepts a configuration object with the following properties:

enforceForClassFields ​

type: boolean

default: true

Enforce this rule for class fields that are functions.

exceptMethods ​

type: string[]

default: []

List of method names to exempt from this rule. Names can include the hash for private methods. Example: save, #rerender

ignoreClassesWithImplements ​

type: "all" | "public-fields"

default: null

Whether to ignore classes that implement interfaces.

ignoreOverrideMethods ​

type: boolean

default: false

Whether to ignore methods that are overridden.

How to use ​

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "class-methods-use-this": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "class-methods-use-this": "error",
  },
});
bash
oxlint --deny class-methods-use-this

Version ​

This rule was added in v1.16.0.

References ​