TS-Scribe - v0.6.2
    Preparing search index...

    Function objectDeepFreeze

    • Recursively freezes an object and its properties, making them immutable. This function ensures that not only the object itself is frozen, but all nested objects and arrays are also frozen. It also makes sure that any changes to the object's properties will result in an error (in strict mode).

      Type Parameters

      • T extends Record<string, unknown>

        The type of the object to be frozen.

      Parameters

      • object: T

        The object to deeply freeze.

      Returns DeepReadonly<T>

      A deep-frozen version of the object, where all properties are recursively frozen.

      const obj = { a: { b: { c: 1 } } };
      const frozenObj = objectDeepFreeze(obj);

      frozenObj.a.b.c = 2; // Error in strict mode: Cannot assign to read only property 'c'

      // Even nested properties are frozen
      frozenObj.a = { b: { c: 3 } }; // Error in strict mode: Cannot assign to read only property 'a'