Typescript string union vs enum

In this typescript tutorial, we will see the difference between string union vs enum in typescript. Then we will see how we can define string union and enums in typescript, and its difference.

Difference between union and enum in typescript

In TypeScript, enum and unions are both methods to declare a set of possible values, but they have some key differences.

An enum in typescript is a type that represents a set of named values, each with an associated numeric or string value. Enums are defined using the enum keyword followed by a set of named values. For example:

enum Color {
    Yellow = 1,
    Green = 2,
    Blue = 3
  }

In the above example, Color is an enum with three named values that are assigned numeric values of 1, 2, and 3 respectively. You can also assign string values to enum members:

enum Color{
Yellow = "yellow",
 Green = "green",
 Blue = "blue"
}

In the above example, Color is an enum with three named values that are assigned string values of “yellow”, “green”, and “blue” respectively.

A union is a type that represents a value that can be of any of multiple kinds. Unions are defined by separating the possible kinds with the ‘|’ character. As an example:

type unionType = number | string | boolean;

In the above example, the ‘unionType’ is a union type can be string, number or boolean.

So the difference between union and enum, is that an enum will allow you to declare a set of named values with associated values, while unions allow you to define a type that can be one of several possible types.

Enums are typically used when you want to define a set of related, named values, while unions are typically used when you want to allow a variable or parameter to have more than one possible type.

How we can define string union and enums in typescript

Here we will see how we can define string union and enums in typescript. Also, we will see the difference between string union and enums.

TypeScript allows you to build string unions and enums to specify a collection of possible string values. String unions and enums are both helpful for assuring that a variable or parameter only accepts certain string values.

String unions are types that represent a collection of possible string values. It is defined by splitting the possible values with the | character. For example, the following string union type shows the possible values of a traffic light:

type trafficLight= "green"|"yellow"|"red"

An enum is a type that represents a collection of named values, each with a string value. Enums are defined by using the enum keyword followed by a series of named values. For example, the following enum describes the different compass directions:

enum direction={
West="W",
East="E",
South="S",
North="N"
}

The primary distinction between string unions and enums is that enums allow you to associate a named value with a string value. This can improve readability and maintainability by allowing you to refer to certain values by name rather than string value.

Typescript string union vs enum
Typescript string union vs enum

Conclusion

In this typescript tutorial, we saw the difference between union and enum in typescript. Also, we see how we can define string type union and enum.

You may also like the following typescript tutorials:

>