title: TypeScript - pass to constructor entire object
body: <p>I have a class at type script:</p>
  
  <pre><code>export class Child {
    name:string;
    age:number;
  }
  </code></pre>
  
  <p>I want to force class instances to have only properties that class declaration has.</p>
  
  <p>For example, if I get from firebase an object:</p>
  
  <pre><code>myFirebaseService.getChild(id).then(function(child){
    var currentChild = new Child(child);
  }) 
  </code></pre>
  
  <p>So when the object is: {name:"ben", color:"db"}, I want the result to be:</p>
  
  <pre><code>currentChild = {"name":"ben"}
  </code></pre>
  
  <p>Becouse "color" is not field of "child".</p>
  
  <p>I tried this:</p>
  
  <pre><code>export class Child {
    name:string;
    age:number;
  
    constructor(tempChild:Child = null) {
      if (tempChild){
        for (var prop in tempChild) {
          this[prop] = tempChild[prop];
        }
      }
    }
  }
  </code></pre>
  
  <p>But it is not help. "currentChild" get all of the fields and attached them to the class instance.</p>
  
  <p><strong>(Of course, I can use the following code:</strong></p>
  
  <pre><code>export class Child {
    name:string;
    age:number;
  
    constructor(tempChild:Child = null) {
      if (tempChild){
        this.nam  = tempChild.name;
        this.age =tempChild.ageÏ;
      }
    }
  }
  </code></pre>
  
  <p><strong>, but my truth class has many fields, and I want short code)</strong></p>