Skip to content

Instantly share code, notes, and snippets.

@Rkokie
Created March 25, 2021 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rkokie/bf122ca2b1b153704709db473907b503 to your computer and use it in GitHub Desktop.
Save Rkokie/bf122ca2b1b153704709db473907b503 to your computer and use it in GitHub Desktop.
deepClone class with typescript
export function deepClone<T>(obj: any): T {
if (obj === null || typeof (obj) !== 'object' || 'isActiveClone' in obj) {
return obj;
}
const clone = obj instanceof Date ? new Date(obj) : new obj.constructor();
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
obj.isActiveClone = null;
clone[key] = deepClone(obj[key]);
delete obj.isActiveClone;
}
}
return clone;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment