@qntm-code/utils
Version:
A collection of useful utility functions with associated TypeScript types. All functions have been unit tested.
16 lines (15 loc) • 851 B
TypeScript
/**
* Recursively (deep) freezes objects/arrays and freezes nested values (best-effort).
*
* Important: JavaScript cannot universally enforce deep immutability for every built-in.
* This function makes the *returned value* behave as readonly where possible.
*
* Notes on exotic built-ins:
* - `Map`/`Set` internal entries are not made immutable by `Object.freeze`, so this function returns a readonly `Proxy`
* that throws on mutating operations.
* - `Date` and ArrayBuffer views (TypedArrays/DataView/Buffer) have mutating APIs that bypass `Object.freeze`, so this
* function returns a readonly `Proxy` that throws on common mutators.
* - If other code still holds a reference to the original mutable object, it can mutate it directly; `freeze()` cannot
* prevent that.
*/
export declare function freeze<T>(value: T): Readonly<T>;