vendredi 27 mai 2016

Gel all model attributes of a class in TypeScript

I have the following TypeScript class:

export class City {
  name: string;
  fullName: string;
  country: string;
  countryCode: string;
  center: GeoPoint;
}

I need a way of getting all model attributes in runtime. For example:

static init(obj): City {

    let city = new City();

    for (var i in obj) {
      if (i == "exists in City model") {
        city[i] = obj[i];
      }
    }
}

is there a simple way of doing this in TypeScript? I don't want to be required to maintain an array of all model attributes names to check this.





Aucun commentaire:

Enregistrer un commentaire