Skip to content

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

Closed
@paulkoerbitz

Description

@paulkoerbitz

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking ChangeWould introduce errors in existing codeFixedA PR has been merged for this issueHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions