# prefer-readonly

Rule `typescript/prefer-readonly`. Require private members to be marked as `readonly` if they're never modified outside of the constructor

- Source: typescript
- Technologies: TypeScript
- Kind: style
- Needs type information: yes
- Autofix: yes, in at least one linter
- Reach: 2.6M weekly downloads of presets that enable it
- Trend: +92.2% last quarter
- Rank: #1469 by reach, #279 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `@typescript-eslint/prefer-readonly` | no | safe | package `@typescript-eslint/eslint-plugin` | [docs](https://typescript-eslint.io/rules/prefer-readonly) |
| Oxlint | `typescript/prefer-readonly` | no | safe | plugin `typescript`, category style | [docs](https://oxc.rs/docs/guide/usage/linter/rules/typescript/prefer-readonly.html) |
| Biome | `style/useReadonlyClassProperties` | no | unsafe | group style, since 2.1.0 | [docs](https://biomejs.dev/linter/rules/use-readonly-class-properties/) |

## Code examples

### Incorrect: the rule reports this

```ts
class Container {
  // These member variables could be marked as readonly
  private neverModifiedMember = true;
  private onlyModifiedInConstructor: number;
  #neverModifiedPrivateField = 3;

  public constructor(
    onlyModifiedInConstructor: number,
    // Private parameter properties can also be marked as readonly
    private neverModifiedParameter: string,
  ) {
    this.onlyModifiedInConstructor = onlyModifiedInConstructor;
  }
}
```

### Correct: the rule accepts this

```ts
class Container {
  // Public members might be modified externally
  public publicMember: boolean;

  // Protected members might be modified by child classes
  protected protectedMember: number;

  // This is modified later on by the class
  private modifiedLater = 'unchanged';

  public mutate() {
    this.modifiedLater = 'mutated';
  }

  // This is modified later on by the class
  #modifiedLaterPrivateField = 'unchanged';

  public mutatePrivateField() {
    this.#modifiedLaterPrivateField = 'mutated';
  }
}
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import tseslint from "typescript-eslint";

export default [
  {
    plugins: { "@typescript-eslint": tseslint.plugin },
    rules: {
      "@typescript-eslint/prefer-readonly": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "rules": {
    "typescript/prefer-readonly": "error"
  }
}
```

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "style": {
        "useReadonlyClassProperties": "error"
      }
    }
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Ultracite for Biome](https://lintradar.dev/presets/ultracite-biome.md) | Biome | `ultracite` | 616 | 2.1M | +151.7% |
| [Ultracite for Oxlint](https://lintradar.dev/presets/ultracite-oxlint.md) | Oxlint | `ultracite` | 475 | 2.1M | +151.7% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |
| [eslint-config-love](https://lintradar.dev/presets/love.md) | ESLint | `eslint-config-love` | 258 | 171.7K | +39.3% |

## Related rules

- [typescript/prefer-find](https://lintradar.dev/rules/typescript/prefer-find.md): Enforce the use of Array.prototype.find() over Array.prototype.filter() followed by [0] when looking for a single result
- [typescript/no-unnecessary-qualifier](https://lintradar.dev/rules/typescript/no-unnecessary-qualifier.md): Disallow unnecessary namespace qualifiers
- [typescript/prefer-includes](https://lintradar.dev/rules/typescript/prefer-includes.md): Enforce `includes` method over `indexOf` method
- [typescript/prefer-nullish-coalescing](https://lintradar.dev/rules/typescript/prefer-nullish-coalescing.md): Enforce using the nullish coalescing operator instead of logical assignments or chaining
- [typescript/strict-boolean-expressions](https://lintradar.dev/rules/typescript/strict-boolean-expressions.md): Disallow certain types in boolean expressions
- [typescript/non-nullable-type-assertion-style](https://lintradar.dev/rules/typescript/non-nullable-type-assertion-style.md): Enforce non-null assertions over explicit type assertions

---

Web page: https://lintradar.dev/rules/typescript/prefer-readonly. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
