Challenge - 5 Problems
Heterogeneous Enum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a heterogeneous enum value
What is the output of this TypeScript code snippet?
Typescript
enum Status {
Ready = 1,
Waiting = "WAIT",
Done = 3
}
console.log(Status.Waiting);Attempts:
2 left
💡 Hint
Look at the value assigned to the Waiting member.
✗ Incorrect
The enum member Waiting is explicitly assigned the string "WAIT", so logging Status.Waiting outputs "WAIT".
❓ Predict Output
intermediate2:00remaining
Value of enum member with mixed types
Given this heterogeneous enum, what is the value of Status.Done?
Typescript
enum Status {
Ready = 1,
Waiting = "WAIT",
Done = 3
}
const doneValue = Status.Done;
console.log(doneValue);Attempts:
2 left
💡 Hint
Check the explicit value assigned to Done.
✗ Incorrect
Done is explicitly assigned the numeric value 3, so doneValue is 3.
❓ Predict Output
advanced2:00remaining
Reverse mapping behavior of heterogeneous enums
What happens if you try to get the enum key from a string value in a heterogeneous enum?
Typescript
enum Status {
Ready = 1,
Waiting = "WAIT",
Done = 3
}
console.log(Status["WAIT"]);Attempts:
2 left
💡 Hint
Reverse mapping works only for numeric enum members.
✗ Incorrect
Reverse mapping is only generated for numeric enum members. Since "WAIT" is a string, Status["WAIT"] is undefined.
❓ Predict Output
advanced2:00remaining
Accessing enum keys from numeric values in heterogeneous enums
What is the output of this code?
Typescript
enum Status {
Ready = 1,
Waiting = "WAIT",
Done = 3
}
console.log(Status[3]);Attempts:
2 left
💡 Hint
Reverse mapping works for numeric values.
✗ Incorrect
Since Done is assigned 3, Status[3] returns the key "Done".
🧠 Conceptual
expert3:00remaining
Why are heterogeneous enums discouraged in TypeScript?
Which of the following is the main reason why using heterogeneous enums (mixing string and numeric values) is discouraged in TypeScript?
Attempts:
2 left
💡 Hint
Think about how reverse mapping works in enums.
✗ Incorrect
Heterogeneous enums break reverse mapping for string members, which can cause confusion and bugs.