-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeepFreeze.ts
25 lines (18 loc) · 851 Bytes
/
deepFreeze.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
type DeepReadonly<T> = T extends (...args: Array<unknown>) => unknown ? T : { readonly [P in keyof T]: DeepReadonly<T[P]> };
declare function Freeze<T extends (...args: Array<unknown>) => unknown>(f: T): T;
declare function Freeze<T>(a: T[]): ReadonlyArray<DeepReadonly<T>>;
declare function Freeze<T>(o: T): DeepReadonly<T>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const deepFreeze: typeof Freeze = (object: any): DeepReadonly<any> => {
Object.freeze(object);
const proprieties = Object.getOwnPropertyNames(object);
for (const name in proprieties) {
const value = object[proprieties[name]];
const canFreeze = typeof value === 'object' || typeof value === 'function';
if (value && canFreeze && !Object.isFrozen(value)) {
deepFreeze(value);
}
}
return object;
};
export default deepFreeze;