Enums in TypeScript are a way to define a collection of related values. There are two main types of enums: numeric enums and string enums.
enum Direction {
Up,
Down,
Left,
Right
}
In a numeric enum, if no initial value is provided, the first member is assigned the value 0
, and subsequent members are assigned incrementing values. So, Direction.Up
is 0
, Direction.Down
is 1
, and so on.
enum DirectionString {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT"
}
In a string enum, each member must be initialized with a string literal.
When it comes to default values in enums, we are often concerned with how to set a default value when using an enum in a variable or a function parameter. For example, if we have a function that takes a Direction
enum as a parameter, we might want to provide a default value in case the user doesn’t pass an argument.
enum Direction {
Up,
Down,
Left,
Right
}
function move(direction: Direction = Direction.Up) {
switch (direction) {
case Direction.Up:
console.log("Moving up");
break;
case Direction.Down:
console.log("Moving down");
break;
case Direction.Left:
console.log("Moving left");
break;
case Direction.Right:
console.log("Moving right");
break;
}
}
// Calling the function without an argument will use the default value
move(); // Output: Moving up
// Calling the function with an argument
move(Direction.Down); // Output: Moving down
In this example, the move
function has a parameter direction
of type Direction
with a default value of Direction.Up
. If no argument is passed when calling the function, the default value will be used.
enum Color {
Red,
Green,
Blue
}
class Car {
color: Color = Color.Red;
constructor() {
console.log(`The car's color is ${Color[this.color]}`);
}
}
const myCar = new Car(); // Output: The car's color is Red
Here, the Car
class has a property color
of type Color
with a default value of Color.Red
. When a new Car
object is created, the default color will be used.
When defining enums, use descriptive names that clearly indicate the purpose of each value. This makes the code more readable and easier to understand. For example, instead of using single-letter names, use full words:
// Bad practice
enum E {
A,
B,
C
}
// Good practice
enum Status {
Active,
Inactive,
Pending
}
Enums help us avoid using magic numbers or strings in our code. Instead of hardcoding a number or a string to represent a particular state, use an enum value. This makes the code more maintainable and less error-prone.
// Bad practice
function checkStatus(status: number) {
if (status === 1) {
console.log("Active");
} else if (status === 2) {
console.log("Inactive");
}
}
// Good practice
enum Status {
Active,
Inactive
}
function checkStatusEnum(status: Status) {
if (status === Status.Active) {
console.log("Active");
} else if (status === Status.Inactive) {
console.log("Inactive");
}
}
Enums should represent a single concept or a related set of values. Avoid creating large enums that contain values from different domains. If you find that an enum is becoming too large, consider splitting it into smaller, more focused enums.
TypeScript enums provide type safety. Make sure to use the enum type consistently throughout your code. This helps catch errors at compile time and makes the code more robust.
enum Fruit {
Apple,
Banana,
Orange
}
function eat(fruit: Fruit) {
console.log(`Eating a ${Fruit[fruit]}`);
}
// This will cause a compile-time error
// eat(3);
// This is correct
eat(Fruit.Banana);
Enum default values in TypeScript are a useful feature that can make your code more flexible and easier to use. By understanding the fundamental concepts, usage methods, common practices, and best practices, you can effectively use enums and their default values in your TypeScript projects. Enums not only improve code readability but also provide type safety, which helps catch errors early in the development process.