Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Object.freeze after object creation doesn't error

See original GitHub issue

TypeScript Version: 3.5.1

Search Terms: Object.freeze, freeze, freezing, properties, object, objects, property


const a = Object.freeze({a: 0});
a.a = 1; // Error: Cannot assign read-only property

const b = {b: 0};
b.b = 1; // No error?

const c = {c: {d: 0}};
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

Playground Link:

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

dragomirtitiancommented, Jul 4, 2019

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:

function freezeAndGuard<T>(p: T): p is Readonly<T> {
    return true;

(function () {
    const b = {b: 0};
    if(!freezeAndGuard(b)) return;
    b.b = 1; // Err

    const c = {c: {d: 0}};
    if(!freezeAndGuard(c.c)) return;
    c.c.d = 1; // Err

Playground link

jimmywartingcommented, Nov 26, 2021

currently wish i could refactor this:

export class JSXIdentifier {
    readonly type: string;
    readonly name: string;
    constructor(name: string) {
        this.type = JSXSyntax.JSXIdentifier; = name;

into this:

export class JSXIdentifier {
    type: string;
    name: string;
    constructor(name: string) {
        this.type = JSXSyntax.JSXIdentifier; = name;
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found