I have a numeric enum like below.
enum Rating {
Unknown = 0,
One,
Two,
Three,
Four
}
I need to get alternative string value of enum when I access the enum string like below.
var stringVal = Rating[Rating.One];
The above line should give me "Rating of One" instead of One.
I need to stick to numeric enums and cannot use string enums. Once solution what I could think of is to use string array like below.
const stringRating = ["Unknown Rating", "Rating One", "Rating is Two", "Rating is Three", "Rating is Four"];
export function toString(rating: Rating): string {
return stringValues[rating];
But is there a better way to achieve this in typescript?
I'm expecting something like Description attribute in c# for enums like below
public enum MyEnum
{
[Description("value 1")]
Value1,
[Description("value 2")]
Value2,
[Description("value 3")]
Value3
}