vendredi 23 mars 2018

Retrieve Typescript class properties

If you have a class

export class Address {
  id: number;
  addrLine1: string;
  addrLine2: string;
  ...
}

After you create an object

const address = new Address();

You can skip TypeScript compiler by adding another property to Address class

address['addrLine3'] = 'some value';

I would like to only keep keys that are part of the class Address in the object (at a later time than instantiation).

Meaning, I would like to remove addrLine3 key from the address object.

We could do it if we could list all fields of the class Address reflectively but that doesn't seem possible, or is it?

Or, could there be any other way?

One way would be to create one singleton object of Address class and compare keys against it object, but it's not the best way!





Aucun commentaire:

Enregistrer un commentaire