Start course
Difficulty
Beginner
Duration
19m
Students
858
Ratings
4.5/5
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
Students
22590
Labs
6
Courses
29
Learning Paths
16

Ed is an Outstanding Trainer in Software Development, with a passion for technology and its uses and holding more than 10 years’ experience.

Previous roles have included being a Delivery Manager, Trainer, ICT teacher, and Head of Department. Ed continues to develop existing and new courses, primarily in web design using: PHP, JavaScript, HTML, CSS, SQL, and OOP (Java), Programming Foundations (Python), and DevOps (Git, CI/CD, etc). Ed describes himself as practically minded, a quick learner, and a problem solver who pays great attention to detail. 

Ed’s specialist area is training in Emerging Technologies, within Web Development. Ed mainly delivers courses in JavaScript covering vanilla JS, ES2015+, TypeScript, Angular, and React (the latter is authored by Ed) and has delivered on behalf of Google for PWAs. Ed has also developed a new suite of PHP courses and has extensive experience with HTML/CSS and MySQL. 

Ed is responsible for delivering QA’s Programming Foundations course using the Eclipse IDE. His skillset extends into the DevOps sphere, where he is able to deliver courses based around Agile/Scrum practices, version control, and CI/CD.

Covered Topics