Avoid unnecessary classes containing only static members

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: typescript-best-practices/no-unnecessary-class

Language: TypeScript

Severity: Notice

Category: Best Practices

Description

This rule advises against the unnecessary use of classes that contain only static members, or nothing. In JavaScript, classes are primarily used for object-oriented programming, where each instance of a class has its own state and behavior. Static members, on the other hand, belong to the class itself and not to any instance of the class.

When a class contains only static members, it does not make use of JavaScript’s object-oriented capabilities, and it can be more difficult to understand, test, and maintain than necessary. In order to avoid this issue, consider using regular functions and variables instead of static class members. This makes your code easier to understand and maintain, and it allows you to make better use of JavaScript’s features.

Non-Compliant Code Examples

class Statics {
  static total = 10;

  static doubleTotal() {
    return Statics.total * 2;
  }
}

class DoThing {
  constructor() {
    thing.do();
  }
}

class Useless {}

Compliant Code Examples

export const total = 10;

export function doubleTotal() {
  return total * 2;
}

function doThing() {
  return thing.do();
}

class Foo {
  constructor() {
    this.prop = 'prop';
  }

  getProp() {
    return this.prop;
  }
}

class Foo2 {
  constructor() {
    this.prop = 'prop';
  }
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis