useReadonlyClassProperties
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/useReadonlyClassProperties
- This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
Description
Section titled “Description”Enforce marking members as readonly
if they are never modified outside the constructor.
This rule ensures that class properties, especially private ones, are marked as readonly
if their values
remain constant after being initialized. This helps improve code readability, maintainability, and ensures
immutability where applicable.
It can be configured to check only private members or all class properties.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”class Container { private onlyModifiedInConstructor = 1; constructor( member1: number, ) { this.onlyModifiedInConstructor = onlyModifiedInConstructor; }}
code-block.ts:2:13 lint/nursery/useReadonlyClassProperties FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Member ‘onlyModifiedInConstructor’ is never reassigned.
1 │ class Container {
> 2 │ private onlyModifiedInConstructor = 1;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ constructor(
4 │ member1: number,
ℹ Using readonly improves code safety, clarity, and helps prevent unintended mutations.
ℹ Unsafe fix: Add readonly decorator.
2 │ ····private·readonly·onlyModifiedInConstructor·=·1;
│ +++++++++
class Container { constructor( private constructorParameter: number, ) { }}
code-block.ts:3:16 lint/nursery/useReadonlyClassProperties FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Member ‘constructorParameter’ is never reassigned.
1 │ class Container {
2 │ constructor(
> 3 │ private constructorParameter: number,
│ ^^^^^^^^^^^^^^^^^^^^
4 │ ) {
5 │ }
ℹ Using readonly improves code safety, clarity, and helps prevent unintended mutations.
ℹ Unsafe fix: Add readonly decorator.
3 │ ·······private·readonly·constructorParameter:·number,
│ +++++++++
class Container { private neverModifiedMember = true;}
code-block.ts:2:13 lint/nursery/useReadonlyClassProperties FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Member ‘neverModifiedMember’ is never reassigned.
1 │ class Container {
> 2 │ private neverModifiedMember = true;
│ ^^^^^^^^^^^^^^^^^^^
3 │ }
4 │
ℹ Using readonly improves code safety, clarity, and helps prevent unintended mutations.
ℹ Unsafe fix: Add readonly decorator.
2 │ ····private·readonly·neverModifiedMember·=·true;
│ +++++++++
class Container { #neverModifiedPrivateField = 3;}
code-block.ts:2:5 lint/nursery/useReadonlyClassProperties FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Member ‘#neverModifiedPrivateField’ is never reassigned.
1 │ class Container {
> 2 │ #neverModifiedPrivateField = 3;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ }
4 │
ℹ Using readonly improves code safety, clarity, and helps prevent unintended mutations.
ℹ Unsafe fix: Add readonly decorator.
2 │ ····readonly·#neverModifiedPrivateField·=·3;
│ +++++++++
class Container { private readonly neverModifiedMember = true; private readonly onlyModifiedInConstructor: number; readonly #neverModifiedPrivateField = 3;
public constructor( onlyModifiedInConstructor: number, private readonly neverModifiedParameter: string, ) { this.onlyModifiedInConstructor = onlyModifiedInConstructor; }}
Options
Section titled “Options”checkAllProperties
Section titled “checkAllProperties”Checks whether all class properties (including public and protected) should be analyzed.
By default, checkAllProperties
is set to false
.
{ "options": { "checkAllProperties": true }}
class Example { public constantValue = 42;
constructor(value: number) { this.constantValue = value; }}
class Example { constructor(protected constructorParameter: string) { }}
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useReadonlyClassProperties": "error" } } }}