Skip to content

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.

class Example {
get value() {
return "hello";
}
}
class Example {
get count() {
return 42;
}
}
class Example {
get enabled() {
return true;
}
}

This rule is not configurable.

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.