Skip to main content

typescript/prefer-readonly-array

Require readonly array and tuple type annotations.

Targeted pattern scope

This rule targets mutable array-like annotations, including:

  • T[] and tuple annotations not wrapped in readonly ..., and
  • Array<T> references.

What this rule reports

This rule reports writable array and tuple type annotations.

Why this rule exists

Readonly array/tuple annotations communicate immutability at API boundaries and reduce accidental mutation.

❌ Incorrect

function f(values: string[]) {}

✅ Correct

function f(values: readonly string[]) {}

Behavior and migration notes

This rule reports only and does not provide an autofix.

Common migration targets are readonly T[], readonly [A, B], or ReadonlyArray<T>.

Options

This rule has no options.

Additional examples

function parse(pair: [string, number]): void {}
// ❌ reported

function parseSafe(pair: readonly [string, number]): void {}
// ✅ valid

ESLint flat config example

import etcMisc from "eslint-plugin-etc-misc";

export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/typescript/prefer-readonly-array": "error",
},
},
];

When not to use it

Disable this rule if mutable arrays and tuples are preferred in your API design.

Package documentation

Rule catalog ID: R107

Further reading

Adoption resources

  • Start at warning level in CI, then move to error after cleanup.
  • Use focused codemods/autofix batches per package or directory.