跳转到内容

useReadonlyClassProperties

此内容尚不支持你的语言。

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.

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;
}
}

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) {
}
}
biome.json
{
"linter": {
"rules": {
"nursery": {
"useReadonlyClassProperties": "error"
}
}
}
}