Enums in TypeScript allow you as a developer to define a set of named constants, which could be either numeric or string values.
They can be used to make your code more readable and maintainable by providing meaningful names for values that would otherwise be represented by raw numbers or strings.
A use of Enums can make your code cleaner and more maintainable. For example, we could represent a traffic light in an enum with each value mapping to the action that a driver should take when they see that traffic light.
enum TrafficLight { Green = "GO", Yellow = "CAUTION", Red = "STOP"}
In this example, we define an enum named TrafficLight that has three possible values: Green, Yellow, and Red. Each value is assigned a corresponding string, which represents the action that a driver should take when they see that traffic light.
We can then use this TrafficLight enum to define a function that returns the action that a driver should take based on the current traffic light:
function getAction(light: TrafficLight): string { switch(light) { case TrafficLight.Green: return "Go"; case TrafficLight.Yellow: return "Slow down"; case TrafficLight.Red: return "Stop"; default: throw new Error("Invalid traffic light"); }} console.log(getAction(TrafficLight.Red)); // Output: "Stop"
In this example, we define a function named getAction that takes a TrafficLight argument and returns the corresponding action that a driver should take. By using an enum, we can make it easy to understand what action a driver should take when they see a specific traffic light. We use a switch statement to check the value of the light argument and return the corresponding action.

