ProjectStack
typescript

TS2540: Cannot assign to 'X' because it is a read-only property

You're trying to modify a property that's been marked as readonly in its type definition. Readonly properties can only be set during initialization — any reassignment afterward is a type error.

Common causes

  • Reassigning a property declared as readonly in an interface or class
  • Mutating an object created with as const, which makes all properties deeply readonly
  • Modifying a property on an object typed as Readonly<T> or using Object.freeze()
  • Trying to reassign a class constructor parameter marked as readonly

How to fix it

  1. Remove the readonly modifier if the property should be mutable
  2. Create a new object instead of mutating: const updated = { ...original, property: newValue }
  3. If using as const, only use it when you intend the object to be immutable
  4. Use a mutable copy: const mutable = { ...readonlyObj } to get a writable version

Example

interface Config { readonly baseUrl: string; } const config: Config = { baseUrl: 'https://api.example.com' }; config.baseUrl = 'https://other.com'; // error TS2540: Cannot assign to 'baseUrl' because it is a read-only property.

Reassigning a readonly interface property after the object has been initialized

Browse more errors

The Developer Hub covers 150+ errors across Git, npm, Node.js, Python, TypeScript, and Docker — with plain-English explanations and fix steps.