0
0
Typescriptprogramming~5 mins

Reverse mapping in numeric enums in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is reverse mapping in numeric enums in TypeScript?
Reverse mapping means you can get the name of an enum member by using its numeric value. TypeScript creates this automatically for numeric enums.
Click to reveal answer
intermediate
How does TypeScript create reverse mapping for numeric enums?
TypeScript creates an object where both the name-to-value and value-to-name mappings exist. For example, Enum[Enum.Value] returns the name as a string.
Click to reveal answer
beginner
Can reverse mapping be used with string enums in TypeScript?
No, reverse mapping only works with numeric enums. String enums do not have reverse mapping because string values are not unique keys.
Click to reveal answer
beginner
Example: What will be the output of this code?
enum Color { Red, Green, Blue }
console.log(Color[1]);
The output will be "Green" because the numeric value 1 corresponds to the enum member Green.
Click to reveal answer
intermediate
Why is reverse mapping useful in TypeScript numeric enums?
It helps to get the name of an enum member from its numeric value, which is useful for debugging, logging, or displaying friendly names.
Click to reveal answer
What does reverse mapping in numeric enums allow you to do?
APrevent enums from being changed
BGet the enum member name from its numeric value
CAutomatically assign string values to enums
DConvert string enums to numeric enums
Does reverse mapping work with string enums in TypeScript?
ANo, never
BOnly if values are unique
CYes, always
DOnly in TypeScript 4.0+
Given enum Status { Active = 1, Inactive, Pending }, what is Status[2]?
A"Pending"
B"Active"
C"Inactive"
Dundefined
What type of object does TypeScript create for numeric enums?
ABoth name-to-value and value-to-name mappings
BOnly value-to-name mapping
COnly name-to-value mapping
DNo mapping, just constants
Why might reverse mapping be helpful when debugging?
AIt shows the numeric value instead of the name
BIt prevents errors in enum usage
CIt converts enums to strings automatically
DIt lets you see the enum member name from a numeric value
Explain how reverse mapping works in numeric enums in TypeScript and why it is useful.
Think about how you can find the name if you only have the number.
You got /3 concepts.
    Describe the difference between numeric enums and string enums regarding reverse mapping.
    Consider how keys and values are stored in the enum object.
    You got /3 concepts.