Enums

Developed with
QA

Contents

keyboard_tab

The course is part of this learning path

Typescript: Zero to Hero
course-steps
8
certification
2
play-arrow
Start course
Overview
DifficultyBeginner
Duration19m
Students85
Ratings
3/5
starstarstarstar-borderstar-border

Description

This module will introduce you to Simple Types in TypeScript. You’ll learn about Arrays, Tuples, and Type Assertion, too.

Learning Objectives

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

Intended Audience

This learning path is aimed at all who wish to learn how to use TypeScript.

Prerequisites

It is essential you understand the face of contemporary web development to attend this course. You should have a good working knowledge of Javascript. Please note: Before attending this class delegates must have a Microsoft account (signing up one is free).

Feedback

We welcome all feedback and suggestions - please contact us at qa.elearningadmin@qa.com to let us know what you think.

Transcript

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.

Lectures

Simple Types: An Introduction - Arrays - Tuples - Any and Unknown - Null and Unidentified - Function Arguments and Return Types - Type Assertion 

About the Author
Students2858
Labs8
Courses29
Learning paths6

An outstanding trainer in software development with more than 15 years experience as a Corporate and Apprentice Trainer, ICT Teacher and Head of Department, with a passion for technology and its uses. Continuing to develop existing and new skills and courses, primarily in web design using PHP, JavaScript, HTML, CSS and SQL but also OOP (Java), programming foundations (Using Python), DevOps (Git, CI/CD, etc) and Agile/Scrum. Practically minded, a quick learner and a problem solver with an attention to detail to ensure high quality outcomes.

Covered Topics