Skip to content

useThisInClassMethods

biome.json
{
"linter": {
"rules": {
"nursery": {
"useThisInClassMethods": "error"
}
}
}
}

Enforce that class methods utilize this.

Instance methods usually communicate that their behavior depends on instance state. When a class member never uses this, it can often be made static or moved outside the class to better reflect its intent.

This rule checks instance methods, getters, setters, and instance field initializers whose value is an arrow function or function expression. Constructors, static members, and static blocks are ignored.

class A {
foo() {
console.log("Hello");
}
}
code-block.js:2:5 lint/nursery/useThisInClassMethods ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Expected this to be used by class method foo.

1 │ class A {
> 2 │ foo() {
^^^
3 │ console.log(“Hello”);
4 │ }

Adding functions as class members can be a convenient way to organize code related to a class, but when those functions do not use instance state, it can be misleading to readers. Consider whether static or a module-level function would better communicate the intent of this code.

If this member does not depend on instance state, make it static or move it outside the class.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

class A {
foo() {
this.value = "Hello";
}
}
class A {
static foo() {}
}

A list of method names to ignore for this rule.

Default: []

biome.json
{
"linter": {
"rules": {
"nursery": {
"useThisInClassMethods": {
"options": {
"ignoreMethods": [
"render",
"#serialize"
]
}
}
}
}
}
}

In this example, render and #serialize are ignored, so the rule does not report these methods.

class Component {
render() {}
#serialize() {}
}

Whether to ignore override methods on subclasses.

Default: false

biome.json
{
"linter": {
"rules": {
"nursery": {
"useThisInClassMethods": {
"options": {
"ignoreOverrideMethods": true
}
}
}
}
}
}

In this example, the method in Derived is ignored because it is marked with override.

abstract class Base {
abstract method(): void;
}
class Derived extends Base {
override method() {} // ignored because it has `override`
}

Controls how classes with an implements clause are handled.

Default: "none"

  • "none" checks classes with an implements clause the same way as any other class.
  • "all" ignores every eligible instance member in classes that implement an interface.
  • "public-fields" ignores only public eligible members in those classes. Protected and private members are still checked.
biome.json
{
"linter": {
"rules": {
"nursery": {
"useThisInClassMethods": {
"options": {
"ignoreClassesWithImplements": "all"
}
}
}
}
}
}

In this example, every eligible member in a class with an implements clause is ignored.

interface Service {
run(): void;
}
class ServiceImpl implements Service {
run() {}
}
biome.json
{
"linter": {
"rules": {
"nursery": {
"useThisInClassMethods": {
"options": {
"ignoreClassesWithImplements": "public-fields"
}
}
}
}
}
}

In this example, only public eligible members are ignored, so helper is still reported.

interface Service {
run(): void;
}
class ServiceImpl implements Service {
run() {}
protected helper() {}
}
code-block.ts:7:15 lint/nursery/useThisInClassMethods ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Expected this to be used by class method helper.

5 │ class ServiceImpl implements Service {
6 │ run() {}
> 7 │ protected helper() {}
^^^^^^
8 │ }
9 │

Adding functions as class members can be a convenient way to organize code related to a class, but when those functions do not use instance state, it can be misleading to readers. Consider whether static or a module-level function would better communicate the intent of this code.

If this member does not depend on instance state, make it static or move it outside the class.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.