Skip to content

typescript/no-extraneous-class Suspicious

What it does

This rule reports when a class has no non-static members, such as for a class used exclusively as a static namespace. This rule also reports classes that have only a constructor and no fields. Those classes can generally be replaced with a standalone function.

Why is this bad?

Users who come from a OOP paradigm may wrap their utility functions in an extra class, instead of putting them at the top level of an ECMAScript module. Doing so is generally unnecessary in JavaScript and TypeScript projects.

Wrapper classes add extra cognitive complexity to code without adding any structural improvements

Whatever would be put on them, such as utility functions, are already organized by virtue of being in a module.

As an alternative, you can import * as ... the module to get all of them in a single object. IDEs can't provide as good suggestions for static class or namespace imported properties when you start typing property names

It's more difficult to statically analyze code for unused variables, etc. when they're all on the class (see: Finding dead code (and dead types) in TypeScript).

Example

ts
class StaticConstants {
  static readonly version = 42;

  static isProduction() {
    return process.env.NODE_ENV === "production";
  }
}

class HelloWorldLogger {
  constructor() {
    console.log("Hello, world!");
  }
}

abstract class Foo {}

References

Released under the MIT License.