Skip to content

noStaticOnlyClass

诊断类别:lint/complexity/noStaticOnlyClass

¥Diagnostic Category: lint/complexity/noStaticOnlyClass

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

此规则报告类没有非静态成员的情况,例如专门用作静态命名空间的类。

¥This rule reports when a class has no non-static members, such as for a class used exclusively as a static namespace.

来自 OOP 范式的用户可能会将其实用函数封装在一个额外的类中,而不是将它们放在 ECMAScript 模块的顶层。在 JavaScript 和 TypeScript 项目中通常不需要这样做。

¥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.

  • 当你开始输入属性名称时,IDE 无法为静态类或命名空间导入的属性提供很好的建议

    ¥IDEs can’t provide as good suggestions for static class or namespace imported properties when you start typing property names

  • 当所有未使用的变量等都在类上时,静态分析代码会更加困难(参见:在 TypeScript 中查找死代码(和死类型))。

    ¥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).

¥Examples

¥Invalid

class X {
static foo = false;
static bar() {};
}
code-block.js:1:1 lint/complexity/noStaticOnlyClass ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid classes that contain only static members.

> 1 │ class X {
^^^^^^^^^
> 2 │ static foo = false;
> 3 │ static bar() {};
> 4 │ }
^
5 │

Prefer using simple functions instead of classes with only static members.

class StaticConstants {
static readonly version = 42;
static isProduction() {
return process.env.NODE_ENV === 'production';
}
}
code-block.js:2:10 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

‘readonly’ modifier can only be used in TypeScript files

1 │ class StaticConstants {
> 2 │ static readonly version = 42;
^^^^^^^^
3 │
4 │ static isProduction() {

¥Valid

const X = {
foo: false,
bar() {}
};
export const version = 42;
export function isProduction() {
return process.env.NODE_ENV === 'production';
}
function logHelloWorld() {
console.log('Hello, world!');
}
class Empty {}

¥Notes on Mutating Variables

你需要注意的一种情况是导出可变变量。虽然类属性可以在外部变异,但导出的变量始终是常量。这意味着导入器只能读取分配给它们的第一个值,而不能写入变量。

¥One case you need to be careful of is exporting mutable variables. While class properties can be mutated externally, exported variables are always constant. This means that importers can only ever read the first value they are assigned and cannot write to the variables.

需要写入导出变量的情况非常罕见,通常被认为是代码异味。如果你确实需要它,你可以使用 getter 和 setter 函数来实现它:

¥Needing to write to an exported variable is very rare and is generally considered a code smell. If you do need it you can accomplish it using getter and setter functions:

export class Utilities {
static mutableCount = 1;
static incrementCount() {
Utilities.mutableCount += 1;
}
}
code-block.js:1:8 lint/complexity/noStaticOnlyClass ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid classes that contain only static members.

> 1 │ export class Utilities {
^^^^^^^^^^^^^^^^^
> 2 │ static mutableCount = 1;
> 3 │ static incrementCount() {
> 4 │ Utilities.mutableCount += 1;
> 5 │ }
> 6 │ }
^
7 │

Prefer using simple functions instead of classes with only static members.

改为执行以下操作:

¥Do this instead:

let mutableCount = 1;
export function getMutableCount() {
return mutableField;
}
export function incrementCount() {
mutableField += 1;
}

¥Related links