freezeState
functionsignature
freezeState<T>(obj, seen): DeepReadonly<T>Deep-freezes a value **in place** and returns it as `DeepReadonly<T>`.
Type parameters
| Name | Type | Description |
|---|---|---|
T | — | The input value type to freeze. |
Parameters
| Name | Type | Description |
|---|---|---|
obj | T | Any value; objects and arrays are frozen recursively. |
| Name | Type | Description |
|---|---|---|
seen | WeakSet<object> | (Advanced) A WeakSet used to track visited objects for cycle/alias safety. |
Returns
DeepReadonly<T> — The **same** reference as obj, but frozen and typed as DeepReadonly<T>.
Example
example
const state = { user: { name: 'Ada' }, items: [1, { id: 1 }] };
const frozen = freezeState(state);
Object.isFrozen(frozen); // true
Object.isFrozen(frozen.user); // true
Object.isFrozen(frozen.items); // true
Object.isFrozen(frozen.items[1]); // trueExample
example
const a: any = {};
a.self = a; // cycle
freezeState(a); // does not recurse infinitelyExample
example
const o = Object.freeze({ x: 1 });
const out = freezeState(o);
out === o; // true