Tuples

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
Students88
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

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
Students3028
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