typescript/class-literal-property-style Style
作用
强制以一致的风格在类中暴露字面量值。
为什么这不好?
将 readonly 字段和用于同类值的简单字面量 getter 混用, 会使类的 API 不一致,也更难快速浏览。
示例
以下是此规则的错误代码示例(默认 "fields"):
ts
class C {
get name() {
return "oxc";
}
}以下是此规则的正确代码示例:
ts
class C {
readonly name = "oxc";
}配置
此规则接受以下字符串值之一:
"fields"
强制对字面量值使用 readonly 字段。
使用此选项时的错误代码示例:
ts
class C {
get name() {
return "oxc";
}
}使用此选项时的正确代码示例:
ts
class C {
readonly name = "oxc";
}"getters"
强制对字面量值使用 getter。
使用此选项时的错误代码示例:
ts
class C {
readonly name = "oxc";
}使用此选项时的正确代码示例:
ts
class C {
get name() {
return "oxc";
}
}如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"typescript/class-literal-property-style": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"typescript/class-literal-property-style": "error",
},
});bash
oxlint --deny typescript/class-literal-property-style版本
此规则于 v1.47.0 中添加。
