Is there a way to get the name of an enum in typescript, like nameof(FirstEnum)? The following code has an ugly if switch that has to be expanded each time a new enum is defined. Is there a more generic way to achieve the same in typescript?
enum FirstEnum {
First = 0,
Second = 1,
Third = 2,
}
enum SecondEnum {
One,
Two,
}
function translateEnum(type$, val): string {
let lookupKey = '';
if (type$ === FirstEnum) {
lookupKey = `firstenum.${FirstEnum[val]}`;
} else if (type$ === SecondEnum) {
lookupKey = `secondenum.${SecondEnum[val]}`;
} else {
throw new Error('not supported');
}
//lookupkey example: secondenum.One
const result = ''; //translate here, ex. await translationService.translate(lookupkey);
return result;
}
translateEnum(SecondEnum , SecondEnum.One);