Skip to content

Instantly share code, notes, and snippets.

@jasonhodges
Created April 19, 2017 13:27
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 jasonhodges/82be015cd49303f20b74f820acba8ca2 to your computer and use it in GitHub Desktop.
Save jasonhodges/82be015cd49303f20b74f820acba8ca2 to your computer and use it in GitHub Desktop.
demo TypeScript deepcopy function
// https://egghead.io/lessons/typescript-deep-copy-aka-clone-objects-using-typescript
function deepcopy<T>(o: T): T {
return JSON.parse(JSON.stringify(o));
}
const foo = {
x: {
y: {
z: 123
}
}
};
const bar = deepcopy(foo);
bar.x.y.z = 456;
console.log(foo);
@ujtordai
Copy link

Hi
What about getters if the object has some getter what would you like to copy?
After used the JSON.parse(JSON.stringify(o) the getters will be lost.
What do you think?
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment