Skip to content

Instantly share code, notes, and snippets.

@Gummiees
Last active January 22, 2021 14:34
Show Gist options
  • Save Gummiees/7f4be1233c952de6145e8f4f6cb9d664 to your computer and use it in GitHub Desktop.
Save Gummiees/7f4be1233c952de6145e8f4f6cb9d664 to your computer and use it in GitHub Desktop.
Checks if an object is has all the properties defined.
/**
* Checks if an object is has all the properties defined.
*
* Attention: It does not work recursively.
* @param obj Object to scan.
* @param type Object type.
* @returns Boolean, depending on if all the properties are defined or not.
*/
defined<T>(obj: T, type: new () => T): boolean {
if (!obj) {
return false;
}
let isDefined: boolean = true;
const backupObj: T = new type();
const keys: any[] = Object.keys(backupObj);
keys.forEach((key: string) => {
if (obj[key] === null || obj[key] === undefined || obj[key] === "") {
isDefined = false;
}
});
return isDefined;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment