Skip to content

extraneousClasses

Reports classes used as static namespaces.

✅ This rule is included in the ts logicalStrict presets.

Classes that contain only static members or only a constructor can be replaced with simpler constructs. Static-only classes are often used as namespaces, but in JavaScript and TypeScript, individual module exports serve this purpose better. Classes with only a constructor can typically be replaced with standalone functions.

Using classes as static namespaces has several drawbacks:

  • Wrapper classes add cognitive complexity without structural improvements
  • Contents are already organized by being in a module
  • IDEs provide better suggestions for module exports than static class properties
  • Static analysis tools can more easily detect unused exports in modules
class Empty {}
class OnlyConstructor {
constructor() {
console.log("init");
}
}
class StaticUtils {
static format(value: string) {
return value.trim();
}
static parse(value: string) {
return JSON.parse(value);
}
}
class Constants {
static readonly VERSION = "1.0.0";
static readonly MAX_SIZE = 100;
}

When set to true, allows empty classes with no members.

// Valid with { allowEmpty: true }
class Empty {}

When set to true, allows classes that contain only a constructor.

// Valid with { allowConstructorOnly: true }
class Example {
constructor() {
console.log("init");
}
}

When set to true, allows classes that only contain static members.

// Valid with { allowStaticOnly: true }
class Utils {
static format(value: string) {
return value.trim();
}
}

When set to true, allows any extraneous class that includes a decorator. This is useful for frameworks that use decorators to configure classes.

// Valid with { allowWithDecorator: true }
@Injectable()
class Service {}

If your codebase uses frameworks that rely heavily on class decorators for configuration (such as Angular or NestJS), you may want to enable allowWithDecorator for consistency. Some legacy codebases may use classes as namespaces extensively, in which case migrating may not be practical.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.