classLiteralProperties
Reports getters that return literal values instead of using readonly class fields.
✅ This rule is included in the ts stylistic preset.
When a class getter only returns a literal value, it can be replaced with a readonly class field.
Readonly fields are more concise and avoid the overhead of setting up and tearing down a function closure.
This rule only reports getters that return constant literal values (strings, numbers, bigints, booleans, regular expressions, template literals, null).
It does not report getters that return objects, arrays, or functions, as these have different semantics.
Examples
Section titled “Examples”class Example { get value() { return "hello"; }}class Example { get count() { return 42; }}class Example { get enabled() { return true; }}class Example { readonly value = "hello";}class Example { readonly count = 42;}class Example { readonly enabled = true;}class Example { get items() { return this.computeItems(); }}Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you are writing a TypeScript library intended to be consumed by JavaScript users, you may prefer getters because the readonly modifier is only enforced at compile time.
JavaScript consumers could still modify a readonly field at runtime.