Arrays

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
Arrays
Overview
DifficultyBeginner
Duration19m
Students86
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

Arrays in TypeScript should be typed, and all element values in it should conform to the type specified. There are two ways TypeScript allows the defining of the type to be done as shown here. The first is to use the array JavaScript type, followed by angle brackets, that contain the type's name. That's shown on line seven. This notation will be familiar, if you've done any C# or Java programming. The second is to use the type name, followed by empty square brackets, as on line 11. Again, this will be familiar to some of you. When defining the values in the array, they should all follow the array's type. As you would expect, the length is not fixed, and adding and removing elements, using push, pop, shift, and unshift, are allowed. We've shown these executed on lines 14 to 17, and all other array functions, available in JavaScript, can be used too. If you try to push a non-numeric value to our number array, say a string, you should at the very least get an error on compilation, and, if you're using an IDE with TypeScript set up in it, like VS Code that we're using, you should get error notation. You can see that we get this on line 20, where we try and push a string. And it tells us that type five is not assignable to parameter of type number. But what would happen if this was compiled? And we ignored the errors? Well yes, you'd get the valid JavaScript that allows this behavior, but if you're expecting an array of numbers to process, and you get a string in the middle, then your application is potentially going to fall over at some point. It's worth noting that destructuring TypeScript arrays works in the same way as it does in JavaScript. But what about situations where we want mixed types in an array? Let's have a look at tuples.

Lectures

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

About the Author
Students3014
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