class-literal-property-style
Enforce that literals on classes are exposed in a consistent style.
在 ESLint 配置 中扩展"plugin:@typescript-eslint/stylistic"
可启用此规则。
此规则报告的一些问题可以通过编辑器 建议 手动修复。
一些 TypeScript 应用使用带有 readonly
修饰符的字段将文字值存储在类上,以防止它们被重新分配。但是,在编写 JavaScript 用户可以使用的 TypeScript 库时,使用 getter
公开这些文字通常更安全,因为 readonly
修饰符是在编译类型时强制执行的。
¥Some TypeScript applications store literal values on classes using fields with the readonly
modifier to prevent them from being reassigned.
When writing TypeScript libraries that could be used by JavaScript users, however, it's typically safer to expose these literals using getter
s, since the readonly
modifier is enforced at compile type.
该规则旨在确保类公开的字面量以上述两种样式之一一致地完成。默认情况下,此规则首选 fields
样式,因为这意味着 JS 不必设置和拆除函数闭包。
¥This rule aims to ensure that literals exposed by classes are done so consistently, in one of the two style described above.
By default this rule prefers the fields
style as it means JS doesn't have to setup & teardown a function closure.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/class-literal-property-style": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/class-literal-property-style": "error"
}
};
在线运行试试这个规则 ↗
选项
该规则接受以下选项:
type Options = [
/** Which literal class member syntax to prefer. */
| 'getters'
/** Which literal class member syntax to prefer. */
| 'fields',
];
const defaultOptions: Options = ['fields'];
¥Options
此规则仅对使用字符串或数字文字初始化的枚举成员强制执行。它不检查对象或数组,因为在这些情况下只读字段的行为与 getter 不同。它也不检查函数,因为使用带有箭头函数值的只读字段作为自动绑定方法是一种常见模式。这是因为这些类型可能会发生变化,并对其使用带来更复杂的影响。
¥This rule only checks for constant literal values (string, template string, number, bigint, boolean, regexp, null). It does not check objects or arrays, because a readonly field behaves differently to a getter in those cases. It also does not check functions, as it is a common pattern to use readonly fields with arrow function values as auto-bound methods. This is because these types can be mutated and carry with them more complex implications about their usage.
"fields"
此样式检查任何返回文字值的 getter 方法,并要求使用带有 readonly
修饰符的字段来定义它们。
¥This style checks for any getter methods that return literal values, and requires them to be defined using fields with the readonly
modifier instead.
带有 fields
样式的代码示例:
¥Examples of code with the fields
style:
- ❌ Incorrect
- ✅ Correct
class Mx {
public static get myField1() {
return 1;
}
private get ['myField2']() {
return 'hello world';
}
}
Open in Playgroundclass Mx {
public readonly myField1 = 1;
// not a literal
public readonly myField2 = [1, 2, 3];
private readonly ['myField3'] = 'hello world';
public get myField4() {
return `hello from ${window.location.href}`;
}
}
Open in Playground"getters"
此样式检查任何分配了文字值的 readonly
字段,并要求将它们定义为 getter。这种样式与 @typescript-eslint/prefer-readonly
规则非常匹配,因为它将识别可以是 readonly
的字段,因此应该将其制成 getter。
¥This style checks for any readonly
fields that are assigned literal values, and requires them to be defined as getters instead.
This style pairs well with the @typescript-eslint/prefer-readonly
rule,
as it will identify fields that can be readonly
, and thus should be made into getters.
带有 getters
样式的代码示例:
¥Examples of code with the getters
style:
- ❌ Incorrect
- ✅ Correct
class Mx {
readonly myField1 = 1;
readonly myField2 = `hello world`;
private readonly myField3 = 'hello world';
}
Open in Playgroundclass Mx {
// no readonly modifier
public myField1 = 'hello';
// not a literal
public readonly myField2 = [1, 2, 3];
public static get myField3() {
return 1;
}
private get ['myField4']() {
return 'hello world';
}
}
Open in Playground何时不使用它
¥When Not To Use It
当你没有强烈的偏好,或者不希望对你的类公开字面量值的方式强制执行特定样式时。
¥When you have no strong preference, or do not wish to enforce a particular style for how literal values are exposed by your classes.
'## 资源'