@typescript-eslint/no-extraneous-class
Disallow classes that are empty, only static, or only a constructor
Also called typescript-eslint/no-extraneous-class, typescript/no-extraneous-class
A class with only static members is a group of functions and constants, not something you make instances of. Every call needs the MathUtils. prefix, and a bundler keeps the whole class even if you use one function. Export the functions and constants from the module instead. A class with only a constructor is a function, and an empty class does nothing.
A class that extends another class isn't reported. Neither is a class with a constructor that declares properties in its parameters, such as constructor(public x: number) {}.
Configure#
Off by default. To turn it on:
[lint.rules]
"@typescript-eslint/no-extraneous-class" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"@typescript-eslint/no-extraneous-class" = ["error", {
allowConstructorOnly = false,
allowEmpty = false,
allowStaticOnly = false,
allowWithDecorator = false,
}]Examples#
export class MathUtilsUnexpected class with only static properties. {
static readonly TAU = Math.PI * 2;
static clamp(value: number, min: number, max: number) {
return Math.min(Math.max(value, min), max);
}
}class PlaceholderUnexpected empty class. {}
class StartupUnexpected class with only a constructor. {
constructor() {
console.log("started");
}
}export const TAU = Math.PI * 2;
export function clamp(value: number, min: number, max: number) {
return Math.min(Math.max(value, min), max);
}class Counter {
static instances = 0;
count = 0;
}
class NotFoundError extends Error {}
class Point {
constructor(public x: number, public y: number) {}
}Options#
| Option | Default | What it does |
|---|---|---|
allowConstructorOnly | false | Allow a class that has only a constructor. |
allowEmpty | false | Allow a class with no members. |
allowStaticOnly | false | Allow a class that has only static members. |
allowWithDecorator | false | Allow any class that has a decorator. |
Some frameworks configure a class through its decorator, and the body stays empty:
{ "allowWithDecorator": true }No problemsimport { Module } from "./framework";
@Module({ providers: [] })
export class AppModule {}{ "allowStaticOnly": true }No problemsexport class Env {
static readonly isProduction = process.env.NODE_ENV === "production";
}