readonly-keyword
Require readonly modifiers where possible.
Targeted pattern scopeâ
This rule targets class fields, TypeScript property signatures, and TypeScript index signatures.
What this rule reportsâ
- Missing
readonlyon class properties - Missing
readonlyon interface/type property signatures - Missing
readonlyon index signatures
Why this rule existsâ
Readonly properties communicate immutability at the type level and prevent accidental mutation.
â Incorrectâ
interface State {
value: number;
}
â Correctâ
interface State {
readonly value: number;
}
Additional examplesâ
// â Mutable class field and index signature
class Settings {
theme = "dark";
}
interface Cache {
[key: string]: string;
}
// â
Readonly members communicate intent
class Settings {
readonly theme = "dark";
}
interface Cache {
readonly [key: string]: string;
}
ESLint flat config exampleâ
import immutable from "eslint-plugin-immutable-2";
export default [
{
files: ["**/*.{ts,tsx,mts,cts}"],
plugins: { immutable },
rules: {
"immutable/readonly-keyword": "error",
},
},
];
When not to use itâ
Avoid this rule in models that are intentionally mutable during their lifecycle (for example form-draft objects, active-record entities, or mutable adapter DTOs). Enforcing readonly in those domains can push developers toward unnecessary type assertions.
Rule catalog ID: R914