Skip to content
← Back to rules

typescript/class-literal-property-style 风格

An auto-fix is available for this rule.

它的作用

强制对类上暴露的字面量值使用一致的风格。

为什么这是个问题?

对于同一种类型的值,混合使用只读字段和简单的字面量访问器(getter),会使类的 API 不一致,难以快速扫描。

示例

此规则的 错误 示例(默认值为 "fields"):

ts
class C {
  get name() {
    return "oxc";
  }
}

此规则的 正确 示例:

ts
class C {
  readonly name = "oxc";
}

如何使用

通过配置文件或在 CLI 中启用此规则,可以使用:

json
{
  "rules": {
    "typescript/class-literal-property-style": "error"
  }
}
bash
oxlint --deny typescript/class-literal-property-style

参考资料