Skip to main content

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 getters, 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.

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/class-literal-property-style": "error"
}
};
在线运行试试这个规则 ↗

选项

注意

此规则仅检查常量字面量值(字符串、模板字符串、数字、bigint、布尔值、正则表达式、null)。 它不检查对象或数组,因为在这些情况下只读字段的行为与 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:

class Mx {
public static get myField1() {
return 1;
}

private get ['myField2']() {
return 'hello world';
}
}
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:

class Mx {
readonly myField1 = 1;
readonly myField2 = `hello world`;
private readonly myField3 = 'hello world';
}
Open in Playground

何时不使用它

当你没有强烈的偏好,或者不希望对你的类公开字面量值的方式强制执行特定样式时。

英:When you have no strong preference, or do not wish to enforce a particular style for how literal values are exposed by your classes.

选项

该规则接受以下选项

type Options = ['fields' | 'getters'];

const defaultOptions: Options = ['fields'];

资源