TS2540

error TS2540: Cannot assign to ‘name‘ because it is a read-only property.

Broken Code ❌

1
2
const user = { name: "Benny" } as const;
user.name = "Sofia";

Fixed Code ✔️

The user object has been initialized with a const assertion, indicating to the compiler that the object is immutable. If you wish to reintroduce mutability, you have to remove the const assertion:

1
2
const user = { name: "Benny" };
user.name = "Sofia";