Function Arguments and Return Types

Developed with



The course is part of this learning path

Typescript: Zero to Hero
Start course


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.


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).


We welcome all feedback and suggestions - please contact us at to let us know what you think.


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.


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

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