Any and Unknown

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

So what if we want to bypass type checking on our TypeScript code and more importantly, can we express that we don't mind what type is received. One option would be to emit any TypeScript notation at all, but that isn't very explicit. The any type allows the explicit bypassing of type checking. Literally any type can be stored in the variable without causing an assignment error. This may be useful if we have an array or two that we want to have a value in, but we aren't first what type that value is. As you can see here on line six, we've decoded an array with a type of any. So values of any type can be used anywhere in the array. We've also got a two pole that expects the first four elements to be of the divine type and then an optional Lanie, which is an object on declaration. If we have a final optional element of type any, we can then push any type to the end of the two pole as long as the defined types are satisfied. This is because any appears in the original list of allowed types for the two pole. The unknown type is a type safe counterpart of any. Any value is assignable to a variable with the type unknown but unknown isn't assignable to anything but itself and any. Without using type assertion, or control flow based narrowing. We'll look at type assertion later and control flow based narrowing in a moment. To demonstrate what we mean, we have a variable called unknown value its been declared as type unknown without being assigned. In the following lines, we can assign and reassign its value, to absolutely any value we want. You could also define an initial value for an unknown, as shown online 19. what you can't do is perform any operations, or access any properties within TypeScript on an unknown variable, without doing type assertion or narrowing. On line 21 we can't access the length property of another unknown as TypeScript shut can't be sure, that another unknown has that property. Narrowing involves the use of the instanceof, an instance of operators or custom type guard functions. This is how controlled flow based type analysis is done. We can pass argument to a function that can be of the type unknown, and then have logic inside it, either using typeof to check for primitives or instanceof to find out if the data is an instanceof a particular class to respond appropriately. We have a stringy five function that will check for a number type and converted to a string before returning the value. If the data is not of type number, it will check to see if it's an instance of the date class and convert this to a string using date functions so that we always get a string returned. The function returns the value past to JavaScript's string class. If the two conditions we've specified were not met. If the type of an instanceof operators don't give you the checks you need, you can write a custom guard function. We'll leave you to search online for examples of these.

Lectures

Simple Types: An Introduction - Arrays - Tuples - 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