In this blog we will learn about enum , union & difference between string unions & enum in Typescript.

Enum in typescript :

Enums are one of the few features unique to TypeScript and not a type-level extension of Enums are one of the few features unique to TypeScript and not a type-level extension of JavaScript. 
A developer can create a set of named constants using enums. Enums can help you establish a collection of unique cases or make it simpler to document purpose. Both string-based and numeric enums are available in TypeScript.

Enum can be used to group and save related values. String, numeric, or a combination of both can be used as enum values.

Example :

// enum.ts

enum Hight {

 High = High’,

  Low = ‘Low’,

};

console.log(Hight.Up);

Union in TypeScript :

 A value that can belong to any one of several different sorts is represented by a type called a Union. When defining unions, the character “|” is used to indicate divisions between potential types.

Syntax:

 unionType = number | string | boolean;

Difference between Enum & Union:

A union allows you to specify a type that can be one of many possible types, but an enum only allows you to declare a set of named values with associated values.

Diffene enum & String Union :

In TypeScript, you can define a set of probable string values by creating string unions and enums. You can ensure that a variable or parameter only takes particular string values by using enums or string unions.

String unions are types that are collections of potential string values. It is defined by dividing the potential values with the | character. The following string union type.

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.

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.