0
0
Typescriptprogramming~5 mins

String enums in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a string enum in TypeScript?
A string enum is a special type in TypeScript where each enum member is initialized with a string literal. It helps group related string constants under one name.
Click to reveal answer
beginner
How do you declare a string enum in TypeScript?
Use the enum keyword followed by member names and assign string values to each member, like:<br>
enum Colors { Red = "RED", Green = "GREEN", Blue = "BLUE" }
Click to reveal answer
intermediate
Why use string enums instead of numeric enums?
String enums provide readable values which make debugging and logging easier. They also avoid accidental numeric value changes and improve code clarity.
Click to reveal answer
intermediate
Can string enum members have computed values?
No, string enum members must be initialized with string literals or other string enum members. Computed or runtime values are not allowed.
Click to reveal answer
beginner
How do you access a string enum member's value?
You access it by using the enum name and member name, like Colors.Red, which returns the string value assigned to that member.
Click to reveal answer
Which of the following is a correct string enum declaration in TypeScript?
Aenum Direction { Up = 1, Down = 2 }
Benum Direction { Up, Down }
Cenum Direction { Up = true, Down = false }
Denum Direction { Up = "UP", Down = "DOWN" }
What will Colors.Green return if enum Colors { Red = "RED", Green = "GREEN" }?
A"GREEN"
B1
C"Green"
Dundefined
Can you assign a number to a string enum member?
AYes, numbers are allowed in string enums.
BNo, string enum members must be strings.
COnly if the number is converted to a string.
DOnly for the first member.
Which is a benefit of using string enums?
AThey make code less readable.
BThey automatically increment numbers.
CThey provide meaningful string values for debugging.
DThey allow computed values.
Are computed values allowed in string enums?
ANo, string enums require string literals.
BYes, always.
COnly if the computed value is a number.
DOnly if the enum is const.
Explain what a string enum is and why you might use it in TypeScript.
Think about grouping related strings under one name.
You got /3 concepts.
    Describe how to declare and access members of a string enum with an example.
    Show a simple enum with colors or directions.
    You got /4 concepts.