title: Convert vanilla object to class?
body: <p>I'm building some best practices with TypeScript. If I make a web request and I get a JSON object with properties <code>Name</code> and <code>Age</code> is there an easy way to convert it to my class that has properties <code>Name</code> and <code>Age</code> as well as a function say <code>PrintPerson</code>?</p>
  
  <p>I know I could write up a constructor that takes the object and field by field copies it into <code>this</code> so the class has the same data. Here's a trivial example:</p>
  
  <pre><code>interface RawPerson {
      Name: string;
      Age: number;
  }
  
  class Person implements RawPerson {
      Name: string;
      Age: number;
  
    constructor(obj: RawPerson) {
        this.Name = obj.Name;
        this.Age = obj.Age;
    }
  
    PrintPerson() {
        console.log(this.Name + ' is ' + this.Age);
    }
  }
  
  var json = { Name: 'Corey', Age: 26 };
  // What works
  new Person(json).PrintPerson();
  // Something simple I'd like to do (but doesn't work)
  // If this worked, I wouldn't need the Person constructor
  (&lt;Person&gt;json).PrintPerson();
  </code></pre>
  
  <p><a href="http://www.typescriptlang.org/Playground#src=interface%20RawPerson%20%7B%0D%0A%09Name%3A%20string%3B%0D%0A%09Age%3A%20number%3B%0D%0A%7D%0D%0A%0D%0Aclass%20Person%20implements%20RawPerson%20%7B%0D%0A%09Name%3A%20string%3B%0D%0A%09Age%3A%20number%3B%0D%0A%20%20%0D%0A%20%20constructor(obj%3A%20RawPerson)%20%7B%0D%0A%09%20%20this.Name%20%3D%20obj.Name%3B%0D%0A%09%20%20this.Age%20%3D%20obj.Age%3B%0D%0A%20%20%7D%0D%0A%20%20%0D%0A%20%20PrintPerson()%20%7B%0D%0A%09%20%20console.log(this.Name%20%2B%20&#39;%20is%20&#39;%20%2B%20this.Age)%3B%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Avar%20json%20%3D%20%7B%20Name%3A%20&#39;Corey&#39;%2C%20Age%3A%2026%20%7D%3B%0D%0A%2F%2F%20What%20works%0D%0Anew%20Person(json).PrintPerson()%3B%0D%0A%2F%2F%20Something%20simple%20I&#39;d%20like%20to%20do%0D%0A%2F%2F%20If%20this%20worked%2C%20I%20wouldn&#39;t%20need%20the%20Person%20constructor%0D%0A(%3CPerson%3Ejson).PrintPerson()%3B" rel="nofollow">TypeScript Playground</a></p>
  
  <p>Having a constructor copy over every field can get tedious. I'd like to do something simple like cast it and just hope that those functions are magically now there. They aren't. Is there some alternative that would save me from writing this clunky constructor?</p>