- Error
- Reported by the type checker
TS2542
Index signature in type '{0}' only permits reading.
An element of a readonly array or a readonly index signature is assigned to. Copy the data to a mutable value first, or remove readonly.
What it means
Index signatures marked readonly, ReadonlyArray<T> and readonly T[] only allow reading elements. Writing through x[i] = ... is reported.
Example
const names: readonly string[] = ["a", "b"];
names[0] = "c";
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(2,1): error TS2542: Index signature in type 'readonly string[]' only permits reading.
Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.
How to fix
- Make a copy and change that:
const copy = [...names]; copy[0] = "c". - Use
names.with(0, "c")(ES2023) to get a changed copy. - Drop
readonlyif the array is meant to be changed.