Bird
Raised Fist0
Typescriptprogramming~10 mins

Heterogeneous enums in Typescript - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1.

What is a heterogeneous enum in TypeScript?

easy
A. An enum that contains both string and numeric values.
B. An enum that only contains numeric values.
C. An enum that only contains string values.
D. An enum that cannot be used with functions.

Solution

  1. Step 1: Understand enum value types

    Enums in TypeScript can hold numeric or string values.
  2. Step 2: Define heterogeneous enum

    A heterogeneous enum mixes both string and numeric values in the same enum.
  3. Final Answer:

    An enum that contains both string and numeric values. -> Option A
  4. Quick Check:

    Heterogeneous enum = mixed string and number values [OK]
Hint: Remember: mixed types in one enum means heterogeneous [OK]
Common Mistakes:
  • Thinking enums can only have one type of value
  • Confusing heterogeneous with union types
  • Assuming enums can't mix strings and numbers
2.

Which of the following is the correct syntax to declare a heterogeneous enum?

enum Status {
  Active = 1,
  Inactive = "NO"
}
easy
A. Incorrect because enum members must be all strings.
B. Incorrect because enums cannot have string values.
C. Incorrect because enum members must be all numbers.
D. Correct syntax with mixed number and string values.

Solution

  1. Step 1: Check enum member types

    The enum has one numeric value (1) and one string value ("NO").
  2. Step 2: Validate TypeScript enum rules

    TypeScript allows heterogeneous enums mixing numbers and strings.
  3. Final Answer:

    Correct syntax with mixed number and string values. -> Option D
  4. Quick Check:

    Mixed values in enum = valid heterogeneous enum [OK]
Hint: Look for mixed number and string values in enum members [OK]
Common Mistakes:
  • Assuming enums can't have string values
  • Thinking all enum members must be same type
  • Confusing enum syntax with object literals
3.

What will be the output of the following TypeScript code?

enum Mixed {
  Yes = 1,
  No = "NO"
}
console.log(Mixed.Yes);
console.log(Mixed.No);
console.log(Mixed[1]);
medium
A. Yes, NO, 1
B. 1, NO, Yes
C. 1, NO, undefined
D. NO, 1, Yes

Solution

  1. Step 1: Check enum values

    Mixed.Yes is 1, Mixed.No is "NO".
  2. Step 2: Understand reverse mapping

    Numeric enums have reverse mapping, so Mixed[1] returns "Yes".
  3. Final Answer:

    1, NO, Yes -> Option B
  4. Quick Check:

    Numeric enum reverse lookup works, string does not [OK]
Hint: Only numeric enum members have reverse mapping [OK]
Common Mistakes:
  • Expecting string enum members to have reverse mapping
  • Confusing enum member names with values
  • Assuming Mixed[1] returns 1
4.

Identify the error in this heterogeneous enum declaration:

enum Colors {
  Red = "RED",
  Green,
  Blue = 3
}
medium
A. Enums cannot mix string and number values.
B. Blue cannot be assigned a number after a string member.
C. Green must have an explicit string or number value.
D. Red cannot be assigned a string value.

Solution

  1. Step 1: Check enum member values

    Red is assigned a string "RED". Green has no value assigned.
  2. Step 2: Understand enum auto-increment rules

    After a string member, numeric auto-increment is not allowed. Green must have explicit value.
  3. Final Answer:

    Green must have an explicit string or number value. -> Option C
  4. Quick Check:

    Members after string need explicit values [OK]
Hint: After string enum member, next must have explicit value [OK]
Common Mistakes:
  • Assuming auto-increment works after string member
  • Thinking enums can't mix types at all
  • Ignoring missing value for Green
5.

You want to create a heterogeneous enum to represent user roles with numeric IDs and string labels. Which enum declaration correctly achieves this?

enum UserRole {
  Admin = 1,
  Guest = "GUEST",
  Member = 2
}
hard
A. This enum is valid and mixes numbers and strings correctly.
B. This enum is invalid because Guest must be a number.
C. This enum is invalid because Admin and Member must be strings.
D. This enum is invalid because Member cannot be a number after a string.

Solution

  1. Step 1: Analyze enum member assignments

    Admin=1 (number), Guest="GUEST" (string), Member=2 (number). All explicitly assigned.
  2. Step 2: Validate heterogeneous enum rules

    TypeScript allows explicit mixing of numeric and string values in enums.
  3. Final Answer:

    This enum is valid and mixes numbers and strings correctly. -> Option A
  4. Quick Check:

    Explicit assignments allow mixing numbers after strings [OK]
Hint: Explicit numeric assignments work after string members [OK]
Common Mistakes:
  • Thinking numbers cannot follow strings even explicitly
  • Confusing explicit assignments with auto-increment rules
  • Assuming strict ordering prevents mixing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard