0
0
Typescriptprogramming~20 mins

String enums in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
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
1
Define the string enum OrderStatus
Create a string enum called OrderStatus with these exact members and values: Pending = 'Pending', Shipped = 'Shipped', and Delivered = 'Delivered'.
Typescript
Need a hint?

Use the enum keyword and assign string values to each member.

2
Create the variable currentStatus
Create a variable called currentStatus and assign it the value OrderStatus.Pending.
Typescript
Need a hint?

Use let currentStatus: OrderStatus = OrderStatus.Pending; to declare the variable.

3
Write the function getStatusMessage
Write a function called getStatusMessage that takes a parameter status of type OrderStatus. Use a switch statement on status to return these exact messages: for OrderStatus.Pending return 'Your order is pending.', for OrderStatus.Shipped return 'Your order has been shipped.', and for OrderStatus.Delivered return 'Your order was delivered.'.
Typescript
Need a hint?

Use a switch statement to check the status and return the correct message.

4
Print the status message
Write a console.log statement to print the message returned by calling getStatusMessage(currentStatus).
Typescript
Need a hint?

Use console.log(getStatusMessage(currentStatus)); to print the message.