• Error
  • Reported by the type checker

TS2540

Cannot assign to '{0}' because it is a read-only property.

A readonly property or a const enum member is assigned to. Create a new object instead, or remove readonly if the property should change.

What it means

Properties declared readonly, getters without setters, as const objects and the properties of Readonly<T> can't be assigned after the object is created.

Example

index.ts
interface Config {
  readonly port: number;
}
const config: Config = { port: 80 };
config.port = 8080;
ts-r-check -p tsconfig.json --pretty false ts-r output
index.ts(5,8): error TS2540: Cannot assign to 'port' because it is a read-only property.

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

  • Build a new object: const next = { ...config, port: 8080 }.
  • Remove readonly if the property is meant to change.