readonly-array
Prefer readonly arrays over mutable arrays.
Targeted pattern scopeâ
This rule targets TypeScript array types (T[], Array<T>, tuples) and inferred mutable array-typed declarations.
What this rule reportsâ
- Mutable type forms like
string[]andArray<string> - Tuple/array types missing
readonly - Implicit array-typed declarations that should be explicitly readonly
Why this rule existsâ
Readonly array types make mutation intent explicit and reduce accidental in-place updates.
â Incorrectâ
type Items = string[];
const values = [1, 2, 3];
â Correctâ
type Items = readonly string[];
const values: readonly unknown[] = [1, 2, 3];
Additional examplesâ
// â Mutable function parameter
const total = (values: number[]) => values.reduce((sum, value) => sum + value, 0);
// â
Readonly parameter prevents accidental writes
const total = (values: readonly number[]) =>
values.reduce((sum, value) => sum + value, 0);
ESLint flat config exampleâ
import immutable from "eslint-plugin-immutable-2";
export default [
{
files: ["**/*.{ts,tsx,mts,cts}"],
plugins: { immutable },
rules: {
"immutable/readonly-array": "error",
},
},
];
When not to use itâ
If your code intentionally mutates arrays as part of low-level algorithms or performance-critical buffers, mandatory readonly annotations can add friction. In those locations, disable the rule locally and keep mutation constrained to well-documented utility modules.
Rule catalog ID: R913