---
description: 'Disallow unused private class members.'
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/no-unused-private-class-members** for documentation.

This rule extends the base [`eslint/no-unused-private-class-members`](https://eslint.org/docs/rules/no-unused-private-class-members) rule.
It adds support for members declared with TypeScript's `private` keyword.

## Options

This rule has no options.

## Examples

<Tabs>
<TabItem value="❌ Incorrect">

```ts
class A {
  private foo = 123;
}
```

</TabItem>
<TabItem value="✅ Correct">

```tsx
class A {
  private foo = 123;

  constructor() {
    console.log(this.foo);
  }
}
```

</TabItem>
</Tabs>

## Limitations

This rule does not detect the following cases:

(1) Private members only used via a variable that would require type analysis to resolve the type.

```ts
type T = Foo;
class Foo {
  private prop = 123;

  method1(a: Foo) {
    // ✅ Detected as a usage
    const prop = this.prop;

    // ✅ Detected as a usage (variables with explicit and simple type annotations are handled)
    const otherProp = a.prop;
  }

  method2(a: T) {
    // ❌ NOT detected as a usage (complex type annotation that requires type information to handle)
    const prop = a.prop;
  }
}
```

(2) Usages of the private member outside of the class:

```ts
class Foo {
  private prop = 123;
}

const instance = new Foo();
// ❌ NOT detected as a usage
console.log(foo['prop']);
```

(3) Reassignments of `this` multiple times:

```ts
class Foo {
  private prop = 123;

  foo() {
    const self1 = this;
    const self2 = self1;
    return self2.prop;
  }
}
```

(4) Mutable reassignments of `this`:

```ts
class Foo {
  private prop = 123;
  private parent: Foo | null;

  foo() {
    let self = this;
    while (self.parent != null) {
      self = self.parent;
    }
    return self.prop;
  }
}
```

## When Not To Use It

If you don't want to be notified about unused private class members, you can safely turn this rule off.
