Bird
0
0

Which approach is best?

hard📝 state output Q15 of 15
Angular - TypeScript in Angular
You want to use an enum OrderStatus in Angular to represent these states: Pending, Shipped, Delivered, and Cancelled. You also want to display user-friendly text for each status in your template. Which approach is best?
AUse only strings in enum and display enum keys directly
BDefine enum with numeric values and create a mapping object for display text
CUse separate string constants instead of enum
DUse enum with mixed string and numeric values without mapping
Step-by-Step Solution
Solution:
  1. Step 1: Understand enum usage for fixed states

    Using numeric enums keeps code efficient and clear for logic.
  2. Step 2: Provide user-friendly text separately

    Mapping numeric enum values to display strings allows flexible, readable UI text.
  3. Step 3: Evaluate other options

    Using only strings or constants reduces type safety or clarity; mixed enums without mapping cause errors.
  4. Final Answer:

    Define enum with numeric values and create a mapping object for display text -> Option B
  5. Quick Check:

    Numeric enum + mapping = best practice [OK]
Quick Trick: Use numeric enums plus a display map for UI text [OK]
Common Mistakes:
  • Using mixed enum types without mapping
  • Displaying enum keys directly without user-friendly text
  • Avoiding enums and using loose string constants

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes