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