Typescript interface variable clone

interface, typescript

Solution

You can use Object.assign:

let copyPerson = Object.assign({}, person);
copyPerson.name = "Alice";

console.log(person); // {name: "bob", birthday: "19000909"}
console.log(copyPerson); //  {name: "Alice", birthday: "19000909"}

Problem

I know the `interface` of the typescript is a Type, so if I define an interface, I can use it to define a variable. My question is that, is there any (pre-defined) method to copy a variable of an interface? for example: ``` interface Person { name: string; birthday: string; } let person: Person = <Person>{}; person.name = "bob"; person.birthday = "19000909"; console.dir(person); let copyPerson: Person = <Person>{}; copyPerson = person; copyPerson.name = "Alice"; //then the person's name is also Alice. because the reference of person is passed to copyPerson. ``` after I change the `copyPerson`, the `person` will change too. I know I can just assign the every property of the `person` to the `copyPerson`, but is there any method to make a separate copy of person? (By the way, if it is class, I can `new` an object)

Original source