no-unused-private-class-members
Disallow unused private class members.
This is an "extension" rule that replaces a core ESLint rule to work with TypeScript. See Rules > Extension Rules.
This rule extends the base no-unused-private-class-members rule from ESLint core. This rule extends the base eslint/no-unused-private-class-members rule.
It adds support for members declared with TypeScript's private keyword.
How to Use
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
// Note: you must disable the base rule as it can report incorrect errors
"no-unused-private-class-members": "off",
"@typescript-eslint/no-unused-private-class-members": "error"
}
});
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"no-unused-private-class-members": "off",
"@typescript-eslint/no-unused-private-class-members": "error"
}
};
Try this rule in the playground ↗
Options
See eslint/no-unused-private-class-members's options.
This rule has no options.
Examples
- ❌ Incorrect
- ✅ Correct
class A {
private foo = 123;
}
Open in Playgroundclass A {
private foo = 123;
constructor() {
console.log(this.foo);
}
}
Open in PlaygroundLimitations
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.
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:
class Foo {
private prop = 123;
}
const instance = new Foo();
// ❌ NOT detected as a usage
console.log(foo['prop']);
(3) Reassignments of this multiple times:
class Foo {
private prop = 123;
foo() {
const self1 = this;
const self2 = self1;
return self2.prop;
}
}
(4) Mutable reassignments of this:
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.