fix(core): updating deepCopy typings

This commit is contained in:
Csaba Tuncsik 2022-11-02 16:32:32 +01:00
parent e237dab3ed
commit 100a0f1f3d

View file

@ -1,12 +1,13 @@
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-argument */ /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-argument */
type Primitives = string | number | boolean | bigint | symbol | null | undefined; type Primitives = string | number | boolean | bigint | symbol;
export const deepCopy = <T extends ((object | Date) & { toJSON?: () => string }) | Primitives>( type WithToJSON = (object | Date) & { toJSON?: () => string };
export const deepCopy = <T extends WithToJSON | Primitives>(
source: T, source: T,
hash = new WeakMap(), hash = new WeakMap(),
path = '', path = '',
): T => { ): T => {
let clone: any; let clone: any;
let i: any; let i: string | number;
const hasOwnProp = Object.prototype.hasOwnProperty.bind(source); const hasOwnProp = Object.prototype.hasOwnProperty.bind(source);
// Primitives & Null & Function // Primitives & Null & Function
if (typeof source !== 'object' || source === null || typeof source === 'function') { if (typeof source !== 'object' || source === null || typeof source === 'function') {
@ -28,7 +29,7 @@ export const deepCopy = <T extends ((object | Date) & { toJSON?: () => string })
clone = []; clone = [];
const len = source.length; const len = source.length;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
clone[i] = deepCopy(source[i], hash, path + `[${i as string}]`); clone[i] = deepCopy(source[i], hash, path + `[${i}]`);
} }
return clone; return clone;
} }
@ -37,7 +38,7 @@ export const deepCopy = <T extends ((object | Date) & { toJSON?: () => string })
hash.set(source, clone); hash.set(source, clone);
for (i in source) { for (i in source) {
if (hasOwnProp(i)) { if (hasOwnProp(i)) {
clone[i] = deepCopy((source as any)[i], hash, path + `.${i as string}`); clone[i] = deepCopy((source as any)[i], hash, path + `.${i}`);
} }
} }
return clone; return clone;