Created
March 25, 2021 15:44
-
-
Save Rkokie/bf122ca2b1b153704709db473907b503 to your computer and use it in GitHub Desktop.
deepClone class with typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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