title: typescript, How do I pass an object to a constructor of class for instantiation
body: <p>I have an array object data get from backend api. like: </p>
  
  <pre><code>[
   {name: 'react', age: 4},
   {name: 'angular', age: 4},
   ...
   {name: 'rxjs', age: 2}
  ]
  </code></pre>
  
  <p>And I definite a <code>class</code> and an <code>interface</code>, like this:</p>
  
  <pre><code>interface IBook {
    name: string;
    age: number;
  }
  
  class Book{
    book: IBook;
  
    constructor(book: IBook) {
      this.book = book;
    }
  
    //I can definite some method here: 
  
    getFullName() {
      return this.book.firstName + ' ' + this.book.lastName;
    }
  
    isValid() {
      return book.name.length &gt; 0;
    }
  
  }
  
  //when get the data
  const dataModels = datas.map(data =&gt; {
    return new Book(data);
  });
  </code></pre>
  
  <p>so I can encapsulate some data model methods like <code>book.getFullName()</code></p>
  
  <p>I can use it like this: </p>
  
  <p><code>const fullname = book.getFullName()</code></p>
  
  <p>rather than this:</p>
  
  <p><code>const fullname = book.firstName + ' ' + book.lastName</code>;</p>
  
  <p>Is there a better way to do this? 
  I am not sure my thought is the correct way or not.</p>
  
  <p>The problem is how to convert a <code>js</code> object to <code>ts</code> class model according to a correct way.</p>
  
  <p>Or, just definite the data <code>interface</code>. Is it necessary to convert <code>javascript</code> json data to <code>typescript</code> <code>class</code> model?</p>
  
  <p>-- update -- </p>
  
  <p><strong>especially, the nested data. like this:</strong></p>
  
  <pre><code>const datas = [
   {name: 'react', age: 2, tag: [{id: 1}, {id: 2}]}
  ]
  </code></pre>