Skip to content

useReadonlyClassProperties

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"style": {
"useReadonlyClassProperties": "error"
}
}
}
}

¥Description

强制要求将成员标记为 readonly,前提是成员在构造函数之外从未被修改。

¥Enforce marking members as readonly if they are never modified outside the constructor.

此规则确保类属性(尤其是私有属性)在初始化后如果值保持不变,则将其标记为 readonly。这有助于提高代码的可读性和可维护性,并在适用情况下确保代码的不可变性。

¥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

¥Invalid

class Container {
private onlyModifiedInConstructor = 1;
constructor(
member1: number,
) {
this.onlyModifiedInConstructor = onlyModifiedInConstructor;
}
}
code-block.ts:2:13 lint/style/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/style/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/style/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/style/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;
+++++++++

¥Valid

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

检查是否应分析所有类属性(包括 public 和 protected)。默认情况下,checkAllProperties 设置为 false

¥Checks whether all class properties (including public and protected) should be analyzed. By default, checkAllProperties is set to false.

biome.json
{
"linter": {
"rules": {
"style": {
"useReadonlyClassProperties": {
"options": {
"checkAllProperties": true
}
}
}
}
}
}
class Example {
public constantValue = 42;
constructor(value: number) {
this.constantValue = value;
}
}
code-block.ts:2:12 lint/style/useReadonlyClassProperties  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Member ‘constantValue’ is never reassigned.

1 │ class Example {
> 2 │ public constantValue = 42;
^^^^^^^^^^^^^
3 │
4 │ constructor(value: number) {

Using readonly improves code safety, clarity, and helps prevent unintended mutations.

Unsafe fix: Add readonly decorator.

2 │ ····public·readonly·constantValue·=·42;
+++++++++
class Example {
constructor(protected constructorParameter: string) {
}
}
code-block.ts:2:27 lint/style/useReadonlyClassProperties  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Member ‘constructorParameter’ is never reassigned.

1 │ class Example {
> 2 │ constructor(protected constructorParameter: string) {
^^^^^^^^^^^^^^^^^^^^
3 │ }
4 │ }

Using readonly improves code safety, clarity, and helps prevent unintended mutations.

Unsafe fix: Add readonly decorator.

2 │ ····constructor(protected·readonly·constructorParameter:·string)·{
+++++++++

¥Related links