The course is part of this learning path
This module will introduce you to Simple Types in TypeScript. You’ll learn about Arrays, Tuples, and Type Assertion, too.
The objectives of this module are to provide you with an understanding of:
- What Simple Types are
- How to use Arrays
- How to use Tuples
- How to use Type Assertion
This learning path is aimed at all who wish to learn how to use TypeScript.
We welcome all feedback and suggestions - please contact us at firstname.lastname@example.org to let us know what you think.
Enums allow you to create additional sets of data types that are used in other languages like C#, and they're are a way of giving a friendly name to numeric-sensitive values. You can define an enum using the enum keyword from TypeScript, and then give it a name. The values in the Enum are words but are not strings, and they're contained in curly braces. You can access the enum value which will return a number related to its index in the enum. This is done by calling the enum's name with a dot followed by the value name. This is shown in the top example. We set an enum of color with red, green, and blue in it and then define a variable C. This is type color, and it's set to the value of green, which is one. By default, enum name values start at zero, but this can be changed at the start by defining the number in the value declaration. Numbers increase sequentially after that. We define the start index of red to be four. So, in this case, enum color green returns five. All enum values can be manually given their own unique value, if required, by defining these in the declaration. We've done this in the third example, and green returns eight. The name can also be returned from the enum using its value in square brackets, essentially, accessing the value, and when we ask for index eight, we get the string green returned. This isn't a very practical use of enum, so let's look at one now that we've got an idea of what they can do. A common use of enums is the setting of a binary value with each bit set to one representing a different option chosen. Bitwise operations are used here, and values in the enum shifted using the one bit shift x notation, which shifts the value x number of bits. In the example shown, we've set an enum of options for configuring a new car. The enum values for each option are set by shifting the value one, the number of bits shown after the double angle brackets. Their binary pattern and decimal values are shown in the comments after each enum value. When we define newCar1 on line 13, we define the options value for the car using the enum options values separated by a bitwise or the bar symbol. This creates a binary pattern for all of the options chosen that can be evaluated bit by bit. When we assess these options in the if statement on line 20, we check to see if options has a value, meaning at least on option was included, and then we use the bitwise AND to see if the cruise control option was set. We'll be looking to see if the value index eight is one. We log out dependent upon this and, for newCar1, cruise control option not chosen is logged out as it's not set in the options for it. NewCar2 does have the cruise control option. So, this would log out cruise control option chosen. We hope that this has demonstrated a real life use of enums for you.