Using String Enums in TypeScript
📖 Scenario: You are building a simple app that tracks the status of orders in an online store. Each order can have a status like 'Pending', 'Shipped', or 'Delivered'. To keep your code clear and avoid mistakes, you want to use string enums to represent these statuses.
🎯 Goal: Create a TypeScript program that defines a string enum for order statuses, uses a variable to hold a status, and prints a message based on that status.
📋 What You'll Learn
Define a string enum called
OrderStatus with values Pending, Shipped, and Delivered.Create a variable called
currentStatus and assign it the OrderStatus.Pending value.Write a function called
getStatusMessage that takes a parameter status of type OrderStatus and returns a string message.Use a
switch statement inside getStatusMessage to return different messages for each status.Print the message returned by
getStatusMessage(currentStatus).💡 Why This Matters
🌍 Real World
String enums help represent fixed sets of string values clearly, such as order statuses, user roles, or categories in apps.
💼 Career
Understanding enums is important for writing clean, maintainable TypeScript code in many software development jobs.
Progress0 / 4 steps