Function Arguments and Return Types

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

When declaring a function, TypeScript allows the defining of types to their arguments. It's as simple as adding a colon on the type in the list as shown here on line four. As for tuples unions of types can be used with bar notation, and optional arguments with the question mark notation. You can use defaults by assigning an argument a default value in the function declaration. Specifying the return type of a function is mandatory in strictly typed languages, such as C sharp and Java. And so TypeScript allows this to be implemented too. You'll notice that our CreateGreeting function on line four has a return type specified after the arguments. In this case, it's a string. There's an arrow highlighted here, as the return on line 11 is commented out. But when we reinstate it, this disappears as the return type is satisfied. You can see that we've used a JavaScript feature added by ES2019 in the ternary statements, these are on lines eight and nine, it's called optional chaining. If optional, the variable doesn't exist, the value after is not evaluated and errors are not thrown. This pattern of declaring functions can also be used in arrow functions as shown in line 15 to 23. Calling a function with the correct arguments is okay. Even on line 29, where we supply undefined as the string arguments. In this instance, the default will be used. Just note that null does not produce this effect. The return of each call is shown in the comment beside it. The arrow function calls a code two producing the same results. On line 36 and 37, we show the effect of calling the functions with the incorrect argument types. We get type assignment errors, as you should be expecting by now. As well as being able to return any valid primitive reference or custom type, such as a class or an object shape, as shown in the function on line five, we need to be able to define a non-return type for a function. Void is used in other languages, and it's implemented in TypeScript to indicate that a function should not return a value. It can also be used to represent a variable value of no value if strict null checks has been used in compilation. FunctionNoReturnValue is okay at the moment, as it simply logs out and doesn't return anything. However, if you try and put a return statement in a function that should return void, you'll get a compilation error, as shown by the arrow highlighted under the return statement on line 12. There's a small gotcha though, you can use a function that returns void as return. The console log shown in the arrow function on line 21 is not highlighted as an error as console log actually returns void. Sometimes we don't want a function to return. And TypeScript provides a never type to indicate that a function should not return the control of execution. Never is a subtype of every type, but nothing is a subtype of never. It literally represents the type for values that never occur. This is useful in error handling when you want the program to throw an unhandled error to cause the execution to stop a fatal error if you will. As you can see, here, we've got a fictitious infinite loop. It's completely useless as code, but it demonstrates to never type as this function would never return. The arrow function shown as a return statement, which induces an error. VS code also detects that it has unreachable code. We've got a more realistic fatal error method that throws a new error and doesn't do anything to deal with the error. So this never returns and control is never passed back. This is a more common use.

Lectures

Simple Types: An Introduction - Arrays - Tuples - Any and Unknown - Null and Unidentified - Enums - Type Assertion 

About the Author
Students4203
Labs8
Courses29
Learning paths6

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