Skip to main content

typescript/prefer-readonly-array-parameter

Require readonly array-like types for function and method parameters.

Targeted pattern scope

This rule targets top-level array-like parameter type annotations, including:

  • T[]
  • tuple types ([A, B])
  • Array<T>
  • union/intersection members such as T[] | null

It also checks constructor parameter properties.

What this rule reports

This rule reports parameter annotations that use mutable array-like types.

Why this rule exists

Parameters are API boundaries. Marking array-like parameter types as readonly reduces accidental mutation and clarifies intent for callers and implementers.

❌ Incorrect

function parse(values: string[]) {}

const fn = (pair: [string, number]) => pair[0];

class Store {
constructor(private keys: Array<string>) {}
}

✅ Correct

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

const fn = (pair: readonly [string, number]) => pair[0];

class Store {
constructor(private keys: ReadonlyArray<string>) {}
}

Behavior and migration notes

This rule is autofixable and also provides suggestions.

  • Array<T> is converted to ReadonlyArray<T>.
  • T[] and tuple types are converted to readonly ... forms.

The rule intentionally checks only top-level parameter types (and top-level union/intersection members), not nested object-property types.

Additional examples

function f(values: string[] | null): void {}
// ❌ reported

function f(values: readonly string[] | null): void {}
// ✅ valid

function f(config: { values: string[] }): void {}
// ✅ valid (nested property type is out of scope)

ESLint flat config example

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

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

When not to use it

Disable this rule if your codebase intentionally mutates array-like parameter values and you do not want readonly parameter contracts.

Package documentation

Rule catalog ID: R108

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.