The course is part of this learning path

Start course
Difficulty
Beginner
Duration
19m
Students
763
Ratings
4.5/5
starstarstarstarstar-half
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

Tuples in TypeScript allow arrays of mixed types. The types and their order must be defined on declaration. However we can use union notation, that's bars, to allow different defined types in an element. We can use a question mark at the end of a type to allow the population of the element to be optional. All elements following an optional element must be optional too. Accessing elements in a tuple is the same as for arrays using the index, as is adding to and removing with a subtle difference as we'll see. So here on line 11 we have a tuple to store a string then two numbers and finally an optional number or a string. We can define the values for the tuple as long as we follow the types and order we specify, including the different optional types. On line 20 if we try and change the order then we get errors shown and there'll be a compilation error if we don't fix it. The errors are assign errors. We're still allowed to add and remove from a tuple as long as we add one of the types that's already been declared. If we try to push a non-declared type, as on line 30, we'll get an ID error again which tells us that boolean is not assignable to string or number. What would happen if we ignore the compiler error? We should remember the only lesson we need for TypeScript. We've written valid Java script, so compiling this code makes a valid Java script file but it's now less type safe than if we taken heed of the TypeScript errors. This is the whole reason we might have chosen to use TypeScript in the first place, to make our code more type safe and more robust. De-structuring a tuple is slightly different to that in arrays as the type is transferred for each element. If we declare a tuple as number string boolean with values and then use normal de-structuring, the values and types follow the de-structured variables. A will be of type number and value seven. B will be of type string and value hello. And C will be of type boolean and value true. Trying to change a value that does not match the de-structured type gives an error. As will de-structuring too many values as seen on line 11. Java script's spread up array can be used to get a shorter tuple, or have an empty tuple at the end of the de-structure. Elements can be ignored in as in arrays. This can be seen on line 18 and 19. So now you've seen how to use arrays and tuples why don't go ahead and get some practice?

Lectures

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

About the Author
Students
20473
Labs
6
Courses
29
Learning Paths
14

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