X Tutup
The Wayback Machine - https://web.archive.org/web/20220922010846/https://github.com/microsoft/TypeScript/issues/40719
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"readonly" interface property is not working when modifying instance property using Object.defineProperty #40719

Closed
guybary-wix opened this issue Sep 23, 2020 · 1 comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@guybary-wix
Copy link

guybary-wix commented Sep 23, 2020

I was playing around with TS interface features, and I saw that setting a custom property configuration (Object.defineProperty) on an object that implements an interface with "readonly" is passing TS checks for the "readonly" interface property.

  interface SomeInterface {
    readonly x: number;
  }

  let obj: SomeInterface = { x: 10 };

  Object.defineProperty(obj, 'x', { value: 5 });
  //obj.x = 5; //Error: Cannot assign to 'x' because it is a read-only property.ts(2540)
  console.log(obj.x); //will output 5

Expected behavior:
"Error: Cannot assign to 'x' because it is a read-only property..."
Since readonly property is being modified

Actual behavior:
Readonly property is writable

Related Issues:
#29634

@MartinJohns
Copy link
Contributor

MartinJohns commented Sep 23, 2020

The read-only only applies to access via that interface, but it does not ensure immutability. There's no way to represent immutability in TypeScripts type system.

@andrewbranch andrewbranch added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Sep 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

4 participants
X Tutup