When it comes to Typescript, enums are represented as values on an object. Unlike the enums in Java, each entry in Typescript enums is not treated as its own object instance, and they are not stored in an array which would provide a convenient "number" (as in Java, where it is zero-based and automatically assigned without overrides). Due to this distinction, a native enum in Typescript cannot have both a string value and an integer value simultaneously.
Despite this limitation, you do have the option of storing these values separately:
enum BloodGroup {
OPositive = 1,
ONegative = 2,
APositive = 3,
ANegative = 4,
}
function labelForBloodGroup(group: BloodGroup) {
switch (group) {
case BloodGroup.OPositive:
return "O +ve";
case BloodGroup.ONegative:
return "O -ve";
case BloodGroup.APositive:
return "A +ve";
case BloodGroup.ANegative:
return "A -ve";
}
}
link to playground