Object.freeze after object creation doesn't error
See original GitHub issueTypeScript Version: 3.5.1
Search Terms: Object.freeze, freeze, freezing, properties, object, objects, property
Code
const a = Object.freeze({a: 0});
a.a = 1; // Error: Cannot assign read-only property
const b = {b: 0};
Object.freeze(b);
b.b = 1; // No error?
const c = {c: {d: 0}};
Object.freeze(c.c);
c.c.d = 1; // No error?
Expected behavior:
Cannot assign read-only property
errors for the two lines with “No error?” above.
Actual behavior:
No errors
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Object.freeze() - JavaScript - MDN Web Docs
As an object, an array can be frozen; after doing so, its elements cannot be altered and no elements can be added to...
Read more >What is the reason for making Object.freeze() to fail silently?
Say Object.frozen() throws errors in non-strict mode, the developer wouldn't use it to freeze every object. If he wants to, then that ...
Read more >JavaScript object immutability: Object.freeze vs. Object.seal
When we freeze an object using Object.freeze , it can no longer be modified. Essentially, new properties can no longer be added to...
Read more >JavaScript Immutability – Frozen Objects in JS Explained with ...
You'll get errors when you try changing a frozen object (immutable object) in the JavaScript strict environment. Hold On – doesn't the const ......
Read more >Make Your Objects Unchangeable with Object.freeze()
This is not the case in JavaScript. The const keyword does not create immutability as the name would imply (i.e. no changes allowed),...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Typescript has no way to model the fact that a simple method call changes the type of its parameter.
The only way to model such changes are custom type guards, and these have to be used in some sort of conditional to work:
Playground link
currently wish i could refactor this:
into this: