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

Deleting from readonly dictionary does not give a type error #11480

Closed
paulkoerbitz opened this issue Oct 10, 2016 · 3 comments
Closed

Deleting from readonly dictionary does not give a type error #11480

paulkoerbitz opened this issue Oct 10, 2016 · 3 comments
Labels
Breaking Change Would introduce errors in existing code Fixed A PR has been merged for this issue Help Wanted You can do this Suggestion An idea for TypeScript

Comments

@paulkoerbitz
Copy link
Contributor

TypeScript Version: 2.0.0, 2.0.3, nightly (2.1.0-dev.20161010)

Code

// A *self-contained* demonstration of the problem follows...
'use strict';

interface X { readonly [key: string]: string }

const x: X = Object.freeze({
    "a": "A",
    "b": "B",
    "c": "C"
});

// Error: TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property.
x["a"] = "B";

// No error - but runtime exception
// TypeError: Cannot assign to read only property 'a' of object '#<Object>'
delete x["a"];

Expected behavior:
delete x["a"]; should give a compile-time error (as it predictably leads to a runtime error later on).

Actual behavior:
No compile-time error is given.

Design Limitation (?)
I understand that this may be a design limitation as in #11180, in this case, this is another vote for the --strictReadonlyChecks flag discussed in #11180.

@RyanCavanaugh
Copy link
Member

Seems reasonable to disallow delete operator on any readonly property or index signature

@RyanCavanaugh
Copy link
Member

Approved for PRs. Shouldn't be too complex.

HerringtonDarkholme added a commit to HerringtonDarkholme/TypeScript that referenced this issue Nov 2, 2016
HerringtonDarkholme added a commit to HerringtonDarkholme/TypeScript that referenced this issue Nov 2, 2016
HerringtonDarkholme added a commit to HerringtonDarkholme/TypeScript that referenced this issue Nov 3, 2016
@mhegazy mhegazy added the Breaking Change Would introduce errors in existing code label Dec 22, 2016
@mhegazy mhegazy modified the milestones: TypeScript 2.2, Community Dec 26, 2016
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Dec 26, 2016
mhegazy added a commit that referenced this issue Dec 26, 2016
fix #11480, disallow delete operator on readonly property or index  signature
@mhegazy
Copy link
Contributor

mhegazy commented Dec 26, 2016

thanks @HerringtonDarkholme !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Breaking Change Would introduce errors in existing code Fixed A PR has been merged for this issue Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants