Skip to content

Instantly share code, notes, and snippets.

@cmddavid
Created October 10, 2019 13:03
Show Gist options
  • Save cmddavid/3de6a1ccd0bb549a00c66f4fd59f7596 to your computer and use it in GitHub Desktop.
Save cmddavid/3de6a1ccd0bb549a00c66f4fd59f7596 to your computer and use it in GitHub Desktop.
class Validator {
validateProperties?(props: (keyof Person)[]){
props.forEach((prop:string) => {
if(!this[prop]){
console.error(`Missing "${prop}" property in object`);
}
})
}
}
enum PersonProperties {
NAME = 'name',
AGE = 'age',
EMAIL = 'email'
}
class Person extends Validator {
[PersonProperties.NAME]: string;
[PersonProperties.AGE]: number;
[PersonProperties.EMAIL]?: string;
constructor(obj: Person){
super();
for(const key in obj){
this[key] = obj[key];
}
};
}
const testPerson = new Person({ name: 'Fred', age: 12 }); // mock server response data
testPerson.validateProperties([PersonProperties.NAME,PersonProperties.AGE,PersonProperties.EMAIL]);
// Result => Missing "email" property in object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment