Skip to content
← Back to rules

unicorn/no-static-only-class Pedantic

⚠️ 🛠️ A dangerous auto-fix is available for this rule.

它的作用

禁止仅包含 static 成员的 class 声明。

为什么这不好?

只有 static 成员的 class 应该直接定义为对象。

示例

以下是此规则的错误代码示例:

javascript
class A {
  static a() {}
}

以下是此规则的正确代码示例:

javascript
class A {
  static a() {}

  constructor() {}
}
javascript
const X = {
  foo: false,
  bar() {},
};
javascript
class X {
  static #foo = false; // 私有字段
  static bar() {}
}

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/no-static-only-class": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-static-only-class": "error",
  },
});
bash
oxlint --deny unicorn/no-static-only-class

版本

此规则于 v0.0.16 中添加。

参考资料