Enumerations are typescript-supported data types. Enumerations allow you to define a set of named constants. They make it easier to document intent or create a different set of cases. Enumerations are mostly used in object-oriented programming languages such as Java and C#, and are now available in TypeScript. They are one of the few features of TypeScript that is not a type-level extension of JavaScript. Next I’ll show you the basics of TypeScript enumerations as well as use cases, the various enumerations types, and the next steps of learning.
What are enumerations in TypeScript
Many programming languages (such as C, C#, and Java) have enum data types, whereas JavaScript does not. TypeScript does, however. TypeScript has enumerations based on numbers and strings. TypeScript enumerations allow developers to define a set of named constants. They make it easier to document intent or create a different set of cases.
The syntax for enumeration is as follows:
Enum {Apple, Orange, Banana, centimetre} // use var fruit = centimetre;Copy the code
What do you need to know about using enumerations in TypeScript
First, the values in the enumeration are constants, that is, the enumeration is type-safe, and a compilation error is returned when the value is reassigned. Second, enumerations should be limited to help users create a custom constant system. Enumerations are an object when compiled: create memory-efficient custom constants in JavaScript, using flexible and easy-to-express record intents as a convenience for judging use cases.
enum requestStatus {
success = 200
error = 400
}
Copy the code
let requestStatus;
(function (requestStatus) {
requestStatus[requestStatus["success"] = 200] = "success"
requestStatus[requestStatus["error"] = 400] = "error"
})(requestStatus || (requestStatus = {}));
// requestStatus:
// { '200': 'success', '400': 'error', error: 400, success: 200 }
Copy the code
Types of common enumerations
Numeric enumerations and string enumerations are the most common enumerations we use in TypeScript, and I’ll show you some examples of their features and how to use them.
** Numeric enumeration
Numeric enumerations store numeric values as strings. Let’s define them using the enum keyword. Here’s an example of how to store a set of different types of cars to show enumerations in TypeScript:
enum CarType {
Honda,
Toyota,
Subaru,
Hyundai
}
Copy the code
The enumerated value CarType has four values: Honda, Toyota, Subaru, and Hyundai. Enumeration values start at 0 and increment each member by 1, as shown below:
Honda = 0
Toyota = 1
Subaru = 2
Hyundai = 3
You can initialize the first value yourself if necessary, as shown below:
enum CarType {
Honda = 1,
Toyota,
Subaru,
Hyundai
}
Copy the code
In the example above, Honda initializes the first member with the value 1. The remaining numbers will be incremented by one.
You might be wondering, if I change the last value, will the previous value decrease based on the last value defined? Let’s try it:
enum CarType {
Honda,
Toyota,
Subaru,
Hyundai = 100
}
Copy the code
Unfortunately this does not work, and the value of the current example is:
enum CarType {
Honda, // 1
Toyota, // 2
Subaru, // 3
Hyundai // 100
}
Copy the code
Note: You do not have to assign sequential values to enumerators. You can assign any value you want to it
String enumeration
String enumerations are similar to numeric enumerations, but their enumeration values are initialized with string values instead of numeric values. String enumerations are more readable than numeric enumerations, making it easier to debug your program.
The following example uses the same information as the numerical enumeration example, but is represented as a string enumeration:
Enum CarType {Honda = "Honda ", Toyota =" Toyota ", Subaru = "Subaru ", Hyundai =" Hyundai "} //return TOYOTACopy the code
Note: String enumeration values need to be initialized separately.
Enumeration reverse mapping
Enumerations can retrieve num values using their corresponding enumerator values. Using reverse mapping, you can access the member value and the name of the member value as shown in the following example:
enum CarType {
Honda = 1,
Toyota,
Subaru,
Hyundai
}
CarType.Subaru; //return 3
CarType.["Subaru"]; //return 3
CarType[3]; //return Subaru
Copy the code
CarType[3] returns its member name “Subaru” due to reverse mapping. Let’s look at another example:
enum CarType {
Honda = 1,
Toyota,
Subaru,
Hyundai
}
console.log(CarType)
Copy the code
In the browser console, you will see the following output:
{'1' : 'Honda', '2' : 'Toyota', '3' : 'Hyundai', '4' : 'Hyundai', Honda: 1, Toyota: 2, Subaru: 3, Hyundai: 4}Copy the code
Each value of an enumeration occurs twice in an enumeration object stored internally.
Calculating the enumeration
The value of an enumerator can be a constant or computed value. Take a look at the following example:
enum CarType {
Honda = 1,
Toyota = getCarTypeCode('toyota'),
Subaru = Toyota * 3,
Hyundai = 10
}
function getCarTypeCode(carName: string): number {
if (carName === 'toyota') {
return 5;
}
}
CarType.Toyota; // returns 5
CarType.Subaru; // returns 15
Copy the code
If an enumeration contains both computed and constant members, the uninitialized enumerator will appear first, perhaps after other initialized members with numeric constants. The next example displays an error:
enum CarType {
Toyota = getCarTypeCode('toyota'),
Honda, // Error: Enum member must have initializer
Subaru,
Hyundai = Toyota * 3,
}
Copy the code
You can declare the above enumeration like this:
enum CarType {
Honda,
Hyundai,
Toyota = getCarTypeCode('toyota'),
Subaru = Toyota * 3
Copy the code
That’s the end of this article, by explaining what enumerations are and what we should be aware of when using them. To our usual enumeration types (numeric enumeration, string enumeration), enumeration reverse mapping, compute enumeration. I believe you have a certain understanding of enumeration, if the article does not speak clearly or there is any wrong place welcome to correct, grateful endless.
Recommended reading
The art of encryption
Code signing certificate, so that software really have a name!