0
0
Typescriptprogramming~10 mins

Heterogeneous enums in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Heterogeneous enums
Define enum with mixed values
Assign numeric and string values
Access enum members
Use enum values in code
Check output or behavior
This flow shows how to define an enum with both numbers and strings, then access and use its members.
Execution Sample
Typescript
enum Mixed {
  No = 0,
  Yes = "YES"
}
console.log(Mixed.No);
console.log(Mixed.Yes);
Defines a heterogeneous enum with a number and a string, then prints both values.
Execution Table
StepActionEvaluationResult
1Define enum member NoNo = 0No assigned 0
2Define enum member YesYes = "YES"Yes assigned "YES"
3Access Mixed.NoMixed.No0
4Access Mixed.YesMixed.Yes"YES"
5Print Mixed.Noconsole.log(Mixed.No)Outputs: 0
6Print Mixed.Yesconsole.log(Mixed.Yes)Outputs: YES
💡 All enum members defined and accessed; program ends after printing values.
Variable Tracker
VariableStartAfter 1After 2Final
Mixed.Noundefined000
Mixed.Yesundefinedundefined"YES""YES"
Key Moments - 2 Insights
Why can enum members have different types like number and string together?
Because heterogeneous enums allow mixing types, as shown in execution_table rows 1 and 2 where No is number 0 and Yes is string "YES".
Can we access enum members by their value in heterogeneous enums?
No, reverse mapping only works for numeric members. Here, Mixed.No can be reverse-mapped but Mixed.Yes cannot, as it is a string.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of Mixed.Yes at step 4?
A"YES"
B0
Cundefined
D"No"
💡 Hint
Check the 'Evaluation' and 'Result' columns at step 4 in execution_table.
At which step does the program print the numeric enum member?
AStep 6
BStep 3
CStep 5
DStep 2
💡 Hint
Look for console.log calls in execution_table rows.
If we changed Mixed.No to "NO" (a string), what would happen to reverse mapping?
AReverse mapping would still work for No
BReverse mapping would not work for No
CReverse mapping would work for all members
DReverse mapping would break the enum
💡 Hint
Recall that reverse mapping only works for numeric enum members as explained in key_moments.
Concept Snapshot
Heterogeneous enums mix numbers and strings.
Syntax: enum E { A = 1, B = "text" }
Numeric members support reverse mapping.
String members do not.
Use to represent mixed-type constants.
Full Transcript
This example shows how to create a heterogeneous enum in TypeScript with both numeric and string values. The enum Mixed has No assigned to 0 and Yes assigned to "YES". When accessing Mixed.No, it returns 0, and Mixed.Yes returns "YES". Printing these values outputs 0 and YES respectively. Reverse mapping works only for numeric members, so only Mixed.No supports it. This helps when you want enum members to have different types but still be grouped logically.