Typescript Beginner To Advanced
Typescript Beginner To Advanced
Typescript Beginner To Advanced
Every effort has been made in the preparation of this book to ensure the
accuracy of the information presented. However, the information contained
in this book is sold without warranty, either express or implied. Neither the
author nor C# Corner will be held liable for any damages caused or alleged
to be caused directly or indirectly by this book.
2
About The Author
Rupesh Kahane works as a Senior Software Engineer in MNC company
located at Pune (India). He did a master’s in computers science and
currently having more than 6+ years’ experience in Web Application
development. He is a four-time C# Corner MVP. He has working
knowledge of technologies like ASP.NET MVC 5.0, C#, Angular,
TypeScript, Azure, SQL Server, Entity Framework, LINQ, Kendo UI,
Telerik Controls, TDD etc. He had conducted more than 120 hands on
Seminars, Workshops for College, Schools and Training Institutes. His
main passion is learning new technologies, sharing knowledge and traveling.
3
Contents
Chapter 1: Introduction to TypeScript 2
Chapter 2. Variable Declaration & Operators in TypeScript 13
Chapter 3. Data Types in TypeScript 26
Chapter 4. Statements in TypeScript 52
Chapter 5. enum in TypeScript 57
Chapter 6. Loops & control statements in TypeScript 65
Chapter 7. Function in TypeScript 76
Chapter 8. Interface in TypeScript 83
Chapter 9. Classes in TypeScript 93
Chapter 10. Generics in TypeScript 112
Chapter 11. Advance Types in TypeScript 124
4
Chapter 1: Introduction to TypeScript
This chapter is an introduction to the TypeScript programming language. In this chapter, we will cover
the following:
1. What is TypeScript
2. Why TypeScript
3. TypeScript Installation
4. TypeScript Basics
5. TypeScript and Object-Oriented
1. What is TypeScript?
TypeScript is a superset of JavaScript; i.e., it includes all the elements of JavaScript & some other features
as well. TypeScript is an open-source programming language that is developed and maintained by
Microsoft. When the user trans compiles the TypeScript code, it is converted into JavaScript. The trans
compiler, also known as a source-to-source compiler, is a special type of compiler that converts the source
code of a program into another language.
TypeScript is designed to build large scale Web applications. The extension of the TypeScript file is
fileName.ts. It supports object-oriented features, static typing, type inference, cross platforms &
Intellisense. The latest TypeScript version, 3.8, is available now.
5
2. Why TypeScript?
There are several reasons why one should use TypeScript.
Example:
JavaScript is not strict about type checking while writing our code, which means we can assign any type
of value to our variable.
In the code below for our variable X, we are able to add any values to a variable.
<script>
var X = "Welcome !!!";
console.log(X);
X = 200;
console.log(X);
X = [10,20,30];
console.log(X);
X = false
console.log(X);
</script>
Now, we will run the above code in JavaScript & print the data on the console.
Welcome !!!
200
6
false
Now if we write the code in TypeScript then we will see how strict type checking works.
let X: string;
X = 'Welcome !!!'
console.log(X);
X = 200;
We will get an error message when we are trying to assign a number to a string type as follows, this is
strict type checking.
3. TypeScript Installation
Different ways to install TypeScript are as follows
What is Node.js?
It was developed in 2009 and it is the server-side run environment used in server-side applications. It is
open-source, uses JavaScript and is built on Chrome’s V8 JavaScript engine. In single-page applications,
this is widely used by Node.js. By using Node.js developers can do CRUD operations easily.
To download & install the latest version of Node.js use link https://nodejs.org/en/download/
After installation developers can check the version information using the command
7
node -v
After successful installation, if you would like to know the version information of TypeScript or would like
to check if the installation is done properly or not then use the below command
tsc -v
When you are using Visual Studio 2019, and then download the latest TypeScript packages using NuGet
package manager, it should be associated with the version itself.
When you are using Visual Studio 2017 then download the latest TypeScript SDK from the below site for
Visual Studio 2017 https://www.microsoft.com/en-us/download/details.aspx?id=55258
Download the Visual Studio Code set up from https://code.visualstudio.com and install it. Now open
the appropriate folder in Visual Studio code & add a new file by giving the and extension as .ts
&https://code.visualstudio.com/Docs/editor/setup
4. Basic Syntaxes
8
The below topics will cover
1. Variable Declaration
2. Function calls using Object-Oriented Concepts
3. Comment
4. Compile options
Step 1
Step 2
Create a new file & give the name to the file with extension as .ts (TypeScript)
9
Step 3
Variable declaration:
Here
keyword: We can use let/var keyword depending upon the scope of our variable.
Optional_value: Value to be assigned is optional, we can assign the value later as well.
Example:
Now write the below code to declare a variable. We know the syntax as
console.log(firstNumber);
Now to run the above code, open terminal & use the below commands
10
tsc basicSyntax.ts
After execution of the above command TypeScript compiler will convert this file into a .js file. Here
typescript compiler trans-compiles the file.
node basicSyntax.js
Warnings:
firstNumber = 'Welcome';
Now if you are trying to assign the string value to our variable then it will give us the error / warning as
=> Type '"Welcome"' is not assignable to type 'number'.
A class is a blueprint of newly-created things. Blueprint means that before making things we can write
down the basic things on paper. The class contains characteristics & behavior. Characteristics mean data
11
members which are written inside a class & behavior means a function that is applied to those
characteristics.
I am going to create a class having one function which will take two parameters as a string and that
function will return both the strings as concatenated output/return format.
Image URL
What is Object?
The object is an instance of a class through which we access the member inside of that class, which means
it can access the characteristics & behavior of that class. The object has a lifespan.
Consider that the below class, concatString, contains a function, displayConcatString, which returns a
concatenated string.
Example
class concatString{
console.log(objectStr.displayConcatString('Hello', 'World'));
12
In the above example, I have created an object of class through which we are calling a function,
displayConcatString.
Now to run the application we will use the same commands as above & our output will be like this:
Comments:
1. Single line comment- By using the // sign developers can write a comment whenever required
Example: It is used to maintain the version number of specific file type we can use
2. Multi-line comment- By using the /* */ sign developers can write comment whenever required
Example: It is used to maintain information about developer name, date time, etc in the
application.
/*
Date: 28-02-2020
*/
Compiler Options
Note –These options are not useful in the command line, these are useful in the tsconfig.json
13
1) --out
2) --removeComments
3) --module
4) --w / --watch
I have written some code as below, myModule is the module, and webURL & completeDetails are two
variables that I am combining into a single string to print as output.
module myModule{
console.log(completeDetails);
I am just executing the above code & will see the output
Here I am using --out to copy the output code into another file, also I will use --removeComments to
remove comments in the file. I will use the below command to create new output file as
14
We will see the code in new file newOutputFile.js as
var myModule;
(function (myModule) {
console.log(completeDetails);
Summary
This chapter was an introduction to Microsoft’s new open-source programming language, TypeScript. In
this chapter, we covered what TypeScript is, why we use TypeScript, how to install TypeScript, and how
to build our first TypeScript program. We also learned the fundamentals of TypeScript.
In the next chapter, I will cover variable declaration & operators in TypeScript.
15
16
Chapter 2. Variable Declaration & Operators in TypeScript
Introduction:
TypeScript is a superset of JavaScript, so it supports variable declaration using let and const. Also, it
supports operators to perform the operations while creating the programs. In this chapter, we will learn
Variable & Operators in TypeScript programming language. In this chapter, we will cover the following
Keywords are not allowed as a variable name. Keywords are the reserve words in the TypeScript
language like break, as, any, switch, if, string, else, etc.
Variable declaration:
Here
keyword: We can use the let/var keyword depending upon the scope of our variable.
Type: Data type of variable like the number, string, Boolean, etc.
Optional_value: Value to be assigned which is optional, we can assign the value later as well.
17
Important points related to the naming convention:
Spaces are not allowed while declaring the variable name in TypeScript. It must contain letters, numbers
& may contain underscore (_). It is case sensitive.
In the above code variable first & FIRST are treated as different in TypeScript.
Image URL
We will consider the below real-life example which will give us the information about the highest score
& player name by year.
Year - 1989
Year - 1992
18
Here in real life variable names are the same, but the values are different every year. There are some
conditions to use the variable name. We can use the same variable name in different scope as in the above
example year is different.
Important Points
A variable name should be unique; it is used to store value by the user/developer. The scope depends
upon the usage. Every variable has a type which is used to store specific/mixed type of data depending
upon its data type.
Birth of a child consists of some information that will be used to calculate the population of a city,
state, or country.
To maintain such a record application, we need a person’s name, gender, date of birth, birth time, birth
location, city, state, country, etc. to maintain the record.
The number of children born in every city has different data each day, so variable names have different
data, but the structure remains the same. We can keep these records in such a manner so we can do the
calculation based on this data & generate the result/report/graphs etc.
So we will use some unique variable names such as personName, gender, dateOfBirth, birthTime,
birthLoction, city, state, country, etc.
19
3. Variable Declaration:
Variables can store single values & this can be changed any time during execution of the program. Variable
declaration by using var keyword is as it is in JavaScript.
Example:
Or
Consider the below code snippet, initially car speed is 0, if a car starts, value is true, then car speed value
can be changed to 60. So the value of the variable can be changed any time during execution of the
program.
Here carSpeedvariable is a global variable when it is declared outside the function. Value can be
preserved outside of condition or function as well.
speedLimit variable is the local variable, we can access it within its construct.
var carSpeed = 0;
var carStarts = true;
if(carStarts)
{
function calculateCarSpeed()
20
4. Re-initializing / redeclaring the variables:
We can use the same variable name in a different function/block scope.
We will write code in which two different functions are having the same variable name as “message”&
that is accessed within that function or only in the scope block of the function. It is allowed in TypeScript
/ JavaScript.
function displayMessage()
{
var message = 'Welcome from displayMessage function';
console.log(message);
}
displayMessage();
function displayMessage_1(){
var message = 'Welcome from displayMessage_1 function';
console.log(message);
}
displayMessage_1();
Code explanation:
We have written two functions having different function names; i.e., displayMessage() &
displayMessage_1(), also we have written the same variable name; i.e., message being used in each
function. We are just printing our message variable value on the console.
We will write one function in which we will declare a num2 variable in the if condition. We are accessing
this variable outside if condition / blocked scope is also possible.
We will execute the above functions by passing parameters to function. The function is returning a value
of num2.
We will pass one parameter which is a type of number & print the actual num2 value in the console. The
second parameter in function is by default false. So, while executing this condition if the condition is false
so the function will return undefined.
Now in the second scenario, we will pass two parameters as first is number & second is Boolean value
true. So, the function will return a value of num2, which is accessible outside if condition/block scoped.
To execute the above script using terminal use the below syntax
let & const are the new variable declarations used in TypeScript. These come into the picture to resolve
problems in the var declaration. let is used to declare a block-scoped variable which means anything
declared within the {} is block-scoped. static type checking is possible by using let variable declaration, so
while writing the script the developer can get suggestions/ warnings, etc. const is similar to let, except
we cannot update or re-declare it.
22
Consider the below code snippet for block-scoped:
Code explanation:
Here while printing the message variable value on a console outside the scope i.e. {}, will get an error
Cannot find name message. This means let variables have been block-scoped.
Consider the below code snippet cannot Re-declare a variable in the same scope:
if(true)
{
let a = 100;
let a = 200;
}
Code Explanation:
TypeScript will give us a warning as we cannot redeclare the block-scoped variable ‘a’.
Consider the below code snippet for Re-declaring a variable in a different scope:
23
This code will execute successfully because we have defined the same variable i.e. message in different
scope. After running this code will get output like this:
Welcome to www.CoderFunda.com
Here age variable is of type number. In the second line if we try to assign a string type value it will give us
a warning as Type ‘updated age’ is not assignable to type ‘number’. So here static type checking is also
done in TypeScript.
B) const Declaration
Or
Or
24
const hours;
In the above code, we just have to initialize the const variables, without initializing it will give us warning
for variable hours.
Important Points
num1++;
let num1: number = 100;
3) If the type of variable is decided, then assign the value of that variable to a different type of
variable
6. What is Operator?
An operator is a symbolic sign / single character which is used to operate. Some mathematical operators
are used to perform some operations like addition (+), subtraction (-), multiplication (*) & division (/).
Some other operators are used to perform some operations on numbers, string, etc. like Boolean. In terms
of a programming language, when we are writing or solving any problem, we need to manage the flow of
our data on which we can make a decision. These are called Conditional operators.
Example: Consider a DJ Operator who operates music by using some keys to make volume up /down, bass
increase/decrease, mix the sounds of two songs, etc. For this operator, every key is used as Operators.
25
Image URL
7. Types of Operators
1) Arithmetic Operators
These operators are used to perform the mathematical operations like addition (+), subtraction (-),
multiplication (*) & division (/).
Example: Consider the following two variables having values A having 50, B having 30 & we will perform
addition, subtraction, multiplication & division of these two variables as below. We will see the output
into a console.
Run the above code using the terminal & see the output as
26
Division of two variables using / operator is: 1.6666
2) Assignment Operators
We can assign values to the variables by using these operators. Also, we can declare shorthand
notations/compound assignment operators.
Code Explanation: In the above example (A += 5) means (A = A + 5) , so A variable has value while
initialization is 100. Now our code will execute as A = (Value of A variable) + 5; i.e. A = 100 + 5;
Run the above code using terminal & see the output as
27
We can use multiplication, division, the modular operator (to calculate reminder), increment, decrement,
etc. It works the same as the above addition, subtraction.
3) Relational Operators:
== (Equal To) Operator: To check the values of two variables, if it is true then it will go into a condition.
!= (Not Equal To) Operator: To check whether the value of two variables is equal or not.
> (Greater Than) Operator: To check if the value of the left side variable is greater than the right side
variable, if it is true then it goes into a condition.
< (Less Than) Operator: To check if the value of the left side variable is less than the right side variable, if
it is true then it goes into a condition.
>= (Greater Than Equal To) Operator: To check if the value of the left side variable is greater than or equal
to the right side variable, if it is true then it goes into a condition.
=< (Less Than Equal To) Operator: To check if the value of the left side variable is less than or equal to
the right side variable, if it is true then it goes into a condition.
Example:
28
console.log(''A & B Both are not equal');
}
if(A>B)
{
console.log('A is greater than B');
}
if(A<B)
{
console.log('A is less than B');
}
if(A>=B)
{
console.log('A is greater than or equal to B');
}
if(A<=B)
{
console.log('A is less than or equal to B');
}
Here A has a value of 150 & B has a value 250, so it will match some conditions and print the message
on the console if the condition is true.
If we run the above code snippet then the output will like:
A is less than B
4) Logical Operator:
&& (Logical AND) Operator: This is a logical AND operator, if both the conditions are true i.e. left & right
side then condition will execute.
|| (Logical OR) Operator: This is a logical OR operator, if one of the conditions is true from the left or right
side then the condition will execute.
! (Logical NOT) Operator: This is a logical Not operator, it checks the opposite of the condition.
29
Example:
It will check the conditions according to the operators executing the condition & printing a message on
the console.
If we run the above code snippet then the output will be:
Summary: In this chapter, we covered what variable is, naming conventions, why we use variables,
variable declaration, Re-initializing / re-declaring of variables, blocked scope, use of let & const, Operator
& its types in TypeScript. In the next chapter, I will cover Data Types in TypeScript.
30
Introduction:
As we have seen how we can declare a variable in the previous chapter, now in this chapter we are going
to learn what are different data types in TypeScript & how can we use them all. Data types are the most
important part of every programming language. In this chapter, we will cover the following different data
types
1. number
2. string
3. boolean
4. tuples
5. union
6. any
7. void
8. null & undefined
9. never
Numbers are the digits used in the programming language; a Number is an object which holds the numeric
values & which are meaningful. TypeScript supports decimal, hexadecimal, octal, binary as well whenever
required. In general, suppose we consider a landline or mobile phone & everyone has a unique number
which is a combination of some digits.
Image URL
31
Consider the sample below to initialize the numbers.
In the above example, we can declare the number like A, B, C & assign the value. Will Execute the
above code & we will get this output:.
● Min Max Value: In a programming language, Integer numbers have maximum & minimum values.
We can access it as shown in the below code snippet.
Number.MAX_VALUE
Number.MIN_VALUE
Here in above example value of Max is 1.7976931348623157e+308 & value of Min is 5e-324
● Safe Integer: Whenever we are doing any addition of +1 / subtraction of -1 in below largest integer
number it is unsafe:
Number.MAX_SAFE_INTEGER
Number.MIN_SAFE_INTEGER
● NaN: Whenever the calculation result is not a valid number then it returns as a NaN
var D = 100;
D = Number.NaN;
console.log("Value of D: ",D);
console.log("Square root: ",Math.sqrt(-1));
32
Here in the above example the value of D variable & Square root of -1 will get NaN as
As we have seen with the operators in the previous chapter, we can compare the NaN value and it will get
the result as true or false depending on the condition. Consider the below example
Execute the above code & you will get the output as false.
● Positive & Negative Infinity: For the number we can check the Infinity values manually or using
static class members of Number
console.log(Number.POSITIVE_INFINITY);
console.log(Number.NEGATIVE_INFINITY);
console.log(Number.length);
console.log(Number.POSITIVE_INFINITY === Infinity);
console.log(Number.NEGATIVE_INFINITY === -Infinity);
● Decimal: In Typescript we can define or calculate the number in a decimal format, decimal has a
base 10
let N = 10.50;
console.log("Value of N: ",N);
console.log(.4 + .2);
Execute the above code & you will get the output as
33
● Hexadecimal: Typescript supports defining the Hexadecimal number. Hex having base 16, so
while calculating multiply to the 16th power
Hexadecimal Decimal
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
A 10
B 11
C 12
D 13
E 14
F 15
Suppose we create a variable having value 0x1D9 then we can get actual value a:
0x1D9
34
0x1D9= 473
let M = 0x1D9;
console.log("Value of M: ",M);
● Octal & Binary: Typescript allows us to define a number as octal or binary. Octal has a base 8.
let K =0o477 ;
console.log("Value of K: ",K);
let L = 0b111000;
console.log("Value of L: ",L);
Number Methods: TypeScript supports some inbuilt methods in the number of data types as below.
A. toExponential(): This method will return the exponential notation in string format.
B. toFixed(): This method will return the fixed-point notation in string format.
C. toLocaleString(): This method will convert the number into a local specific representation of the
number.
D. toPrecision(): This method will return the string representation in exponential or fixed-point to
the specified precision.
E. toString(): This method will return the string representation of the number in the specified base.
F. valueOf(): This method will return the primitive value of the number.
Example
Consider the below example in which you will use number methods as explained above.
35
let second = 1001.2589;
console.log("toFixed: ",second.toFixed(2));
console.log("toLocaleString: ",second.toLocaleString());
console.log("toPrecision: ",second.toPrecision(2));
console.log("toString: ",second.toString());
let third = second.valueOf();
console.log("valueOf: ",third);
console.log("typeof: ",typeof third);
We will execute the above code & get the output as:
What is a string?
The string is the sequence of Unicode characters or a continuous array of characters. The string
can be stored in the format of single/double quotation marks. The string has a meaningful valid
name used in the programming language. It can contain an alphabet, numbers, special
characters, etc. In general suppose a person object has some properties like the first name, last
name, email id, address, etc.
36
Image URL
Or
In the above code firstName, lastName & userName are of type string. We can initialize the
string later as well, as lastName we have only declared & initialized later after the userName
variable.
Consider the below example, I am going to declare two variables, at the time of declaration I
have initialized them.
37
console.log("Last Name: ",lastName);
console.log("Access letter using index at position 0: ",firstName[0]);
Execute the above code snippet & you will get output like this:
Important Points:
On the above string object, we can apply the built-in functions like calculate length, make as all characters
as upper case, etc.
string Methods: TypeScript supports some inbuilt methods in the string data types as below.
38
A. charCodeAt() - Returns the Unicode of the character at the specified index
B. fromCharCode() - Converts Unicode values to characters. Due to static object it always uses
String.fromCharCode();
Syntax - String.fromCharCode(CharNum);
CharNum is a numeric value, users can pass multiple values as well.
Example:
C. includes() - This method returns a true or false by checking whether a string contains the
specified string/characters
Example:
39
D. indexOf() - This function returns the position of the first found occurrence of a specified value in
a string
Example:
E. lastIndexOf() - This function returns the position of the last found occurrence of a specified
value in a string; if not found then returns -1.
Example:
let welcomeNote = "Welcome to the https://www.CoderFunda.com/ => One Stop Solutio
n for All Your Learning Needs";
let result = welcomeNote.lastIndexOf("Stop");
console.log(result);
Example:
let wordA = "Sky is blue";
let wordB = "Sky is blue";
console.log(wordA.localeCompare(wordB));
console.log(wordA.localeCompare("Welcome to CoderFunda.com"));
40
The output of the above code is:
G. replace() - This function searches a string for a specified value, or a regular expression, and
returns a new string where the specified values are replaced.
Example:
let myURL = "Do not forget to comment us at www.CoderFunda.com!";
console.log(myURL.replace("comment", "rate"));
I. match() - This function searches a string for a match against a regular expression, and returns
the matches
Example:
let result = "100,200,300,300,400!";
console.log(result.search("200"));
console.log(result.search("300"));
Example:
41
let stringWelcome = "Welcome to hello World !!!";
console.log(stringWelcome.toLowerCase());
console.log(stringWelcome.toUpperCase());
Example:
let myNUmber: number = 1001;
let myString = myNUmber.toString();
console.log(myString);
console.log(typeof(myString));
What is Boolean?
Primitive data type function is used as a Boolean in TypeScript & JavaScript. Boolean can take true or false
values. In general, consider an example of a light bulb, it might be an On/Off, true/false. We can consider
if the light is on then true, else false.
Syntax
Example:
42
console.log(' x has a value', x);
}
Here in the above code x value is of type Boolean & has the value true.
Run the above code & you will get the output as:
Object: It is an instance of a data type/object. TypeScript allows us to create instances of data type.
Here in above code operator ==, checks equality in both type and value.
Run the above code & you will get the output as:
Boolean function: In typescript & javascript Boolean() is a function, it takes a value & returns a
result. In the below example, commented lines return the below result.
console.log(Boolean(false))
// returns false
console.log(Boolean(true))
// returns true
console.log(Boolean("welcome to CoderFunda.com"))
// returns true
console.log(Boolean(100.50))
// returns true
43
console.log(Boolean(NaN))
// returns false
console.log(Boolean(undefined))
// returns false
Example
console.log(Boolean.length);
console.log(Boolean.prototype.constructor);
Boolean Methods: TypeScript supports some inbuilt methods in the Boolean data types as below.
A. toString(): The toString() method returns a string. In the below example for variable
stringA.toString() method returns a string.
44
B. valueOf(): The valueOf() method returns the primitive value of a Boolean object. Consider the
below example.
What is Tuple?
It is a new data type introduced in Typescript. Sometimes the programmer needs to store the
data of a different type, so Array does not satisfy this condition. In tuples, we can store multiple
types of data like number, string, Boolean, etc.
Why Tuple?
Consider a normal variable which stores the single data type value at a time, we can’t assign other
data type values. To store multiple data type values into a single variable is possible using a tuple.
Example:
Consider a computer or laptop, having some configuration like computer/laptop serial number,
manufacturer name, processor, hard disk, RAM, etc.
45
// normal variable declaration
let numberOfComputers: number = 1000;
let manufacturerName: string = "Dell";
// tuple declaration
let computerDetails: [number, string, boolean] = [1000,"Dell", true];
In the above example, computer details are stored in two different variables in a normal variable
declaration, since Tuple is a new data type. So we can store different types of data into a single
variable as above.
Tuple Array: Typescript allows us to declare the tuple array, so the user can store the different
types of data in a single array.
Array Methods
console.log(laptopDetails[0]);
console.log(laptopDetails[1]);
console.log(laptopDetails[2]);
console.log();
console.log(laptopDetails.sort());
console.log();
laptopDetails.forEach(function (value) {
console.log(value);
});
46
In the above example, laptopDetails is a tuple array having some values. We can access it by using
indexes & also we can print the values in tuples by using the sort method of the array.
laptopDetails.push(1023);
laptopDetails.push([10,"Dell","Intel"]);
console.log(laptopDetails.sort());
47
5. Union data type in TypeScript
It allows us to create more than one type. By using Union we can easily add values of different types to
the same variable. Consider our function is returning values based on conditions, then we can declare
the variable using Union. A vertical bar (pipe i.e. symbol | ) is used to declare two or more data types
into a single variable.
Syntax:
Example:
Execute the above code & you will get output like:
Consider the below function. In a function ShowValue we are passing parameter input as a union type of
number & boolean so it can accept number & union value.
48
function ShowValue(input) {
if(typeof(input) == "boolean") {
console.log('Varible has assigned a boolean value', input);
return input;
} else {
console.log('Varible has assigned a other value', input);
return input;
}
}
console.log("Passing a numeric value to function & function returns: ",ShowValue
(5));
console.log();
console.log("Passing a boolean value to function & function returns: ",ShowValue
(true));
Execute the above code & we will get output like this:
Union Type & Array: Union also accepts the array. Properties & interface is also valid in the union.
49
console.log("Value in Array at position "+ i + " is :",parameter[i]);
}
}
}
console.log(getDetails(true));
console.log();
console.log(getDetails([100,200,300]));
Execute the above code & you will get this output:
Consider the below function. In function getDetails will pass a parameter of two dimensional array of
number & string of union type.
Execute the above code & you will get output like this:
50
6. any data type in TypeScript
When the user/programmer is not sure about the data type then, in this case, we can define it as
any type. It can accept any data type values like a number, boolean, string, etc. Sometimes if we
are getting data from any third party application & we are not aware of the return type of that
data then, in this case, we can use any data type. It is very powerful to use in Typescript other
than Javascript. It is representing values with no constraints. The compiler does not have an idea
about its members, it will type check when it will assign or access the members.
Syntax:
Example:
Consider the below example - we have created a variable having type as any. We will assign a
different value to it like string, numeric & boolean, etc.
myVariable= 1000;
console.log("Variable has assigned a numeric type value is: ", myVariable)
myVariable= true;
console.log("Variable has assigned a boolean type value is: ", myVariable)
51
Execute the above code & we will get output like this:
We can define the function which is not yet defined so the compiler will transcompile successfully, which
means the user is telling the compiler don’t worry about it. I will handle it & come back to you. Consider
the below code snippet for more information.
myVariable.getLatestInfomation();
Let's transpire the above code & see the command prompt.
It transpires successfully.
Consider the below code snippet, we have defined parameter variables as any type & at the time of calling
we are passing an array value of the same or different types.
let parameter:any;
function getDetails(parameter)
{
for(let i: number = 0;i<parameter.length;i++)
{
console.log("Value in Array at position "+ i + " is :",parameter[i]);
}
}
console.log(getDetails([100,200,300]));
console.log();
console.log(getDetails(["Sam","Rohan","Prathmesh"]));
console.log();
console.log(getDetails([true,"Welcome",5000]));
52
Execute the above code & you will get output like this:
What is Object?
The object has its life span & it is an Object interface. Only members inside the interfaces are available in
the object. When we write access methods without implementation then we will not transcompile, it
will give us an error. Object exposes the properties, functions defined in the Object.
Syntax:
variable_Name: Object;
Example:
In the below code snippet there are two Objects, result & Person. Person has two properties like Id &
firstName having numeric & string types. We can access properties using the object. Property name will
be assigned to the result object directly.
If we do not implement any function & try to access it by using Object then it will not trans-compile the
code & will give us an error during compile time. Consider the below code snippet:
Ww will try to trans-compile the above code & see the terminal
If we use a void return type of any function, then it means the function does not return any value. The
void is the opposite of any data type, which means the void does not have any value at all. any may return
any kind of data (including null and undefined) so avoid the use of void data type.
Example:
function getEmployeeDetails(): void {
let Id: number = 1000;
let Name: string = 'Sam';
console.log("Employee id is: ", Id);
console.log("Employee name is: ", Name);
}
let result = getEmployeeDetails();
console.log("Variable called");
Execute the above code & you will get output like this:
54
Variable declaration using void:
Example:
1)
let firstName : void;
console.log("Variable value is : ", firstName);
2) Suppose we tried in another way, like consider the below code snippet:
let myResult: void = "Jay";
console.log("myResult variable has a type of : ",typeof(myResult));
Execute the above code & you will get output like this:
These are the same as void, and not used too much in TypeScript / JavaScript. These have their types
named as null & undefined. We can check at the time of strict type checking. It’s better to use any to
avoid many errors in our application.
Example:
let myValue: undefined = undefined;
let myResult: null = null;
console.log("myValue variable has a type of : ",typeof(myValue));
console.log("myResult variable has a type of : ",typeof(myResult));
55
Execute the above code & you will get output like this:
It is a new type of typescript. It means the value will never be assigned to variable or return from function.
never is a subtype of and assignable to every type. It doesn't return undefined, either.
Example:
1) In the below two sentences first id is valid & second name will give us a warning as - Type '"Joy"'
is not assignable to type 'never'
let id: never;
let name: never = "Joy";
2) Consider the below code snippet, the return type of the calculatePrice function is inferred to be
never. Here TypeScript infers the never type.
const calculatePrice = (message: string) => {
throw new Error(message);
};
For void it will allow us to assign undefined, for never it will give us the error. All errors & warnings are
as follows
let firstName: void = undefined;
let middleName: void = null;
let lastName: never = undefined;
let addressLine: never = null;
56
Summary: In this chapter, we covered different data types in TypeScript, how we can declare the variable
using different types & some inbuilt methods in data types which are useful while creating some
programs. In the next chapter, I will cover different types of statements in TypeScript.
57
Chapter 4. Statements in TypeScript
Introduction:
Whenever we are creating an application, at that time we need to handle some conditional blocks, so
statements are very useful in this case. Different types of statements like if, if-else, if – else if –else, etc
can be used in TypeScript. In this chapter, we will cover the following
1. What is a Statement?
2. Why do we need a Statement?
3. if statement
4. if-else statement
5. if-else-if
What is a Statement?
In programming languages, we need to execute the different conditions, so the statements are very
useful. We can use multiple statements while creating a program.
Real-life example:
58
Image Source
If the tea is hot, then we will wait for a little bit of cooling, because we cannot drink hot tea directly.
Here the condition makes sense and is very important.
Programming has many features like conditional statements, conditional expressions, and conditional
constructs, etc. that are useful while building small programs or large applications. To print a large amount
of data using loops or based on conditions, statements are useful. To handle different cases, we can use
conditional statements. To perform logical operations by using operators and different conditions,
statements are a very powerful way.
Selection Statement
1) If statement: If expression/condition is true then it will execute the statement block. In this type of if-
statement, the sub-statement will only be executed if the expression is non-zero. Multiple if blocks are
allowed. if it is a reserved word in TypeScript, we can’t use it as a variable name.
Syntax:
if ( expression ) statement
Example:
59
If condition true, so print successfully
2) if-else statement: If-else statement executes based on the condition, if the expression is satisfied then
the if statement will execute, else statement will execute. Multiple if-else blocks are allowed. Each else
matches up with the closest unmatched if. If & else are the reserved words in TypeScript, we can’t use it
as a variable name.
Syntax:
if ( expression )
{
statement
}else
{
statement
}
Example: Consider the below example, if IsActive is true then if block will execute otherwise else block
will execute.
let IsActive: boolean = true;
if(IsActive)
{
console.log("You are in the If block");
}
else
{
console.log("You are in the else block");
}
Example: Consider the below example, we can write multiple if-else conditions per our requirement.
let IsActive: boolean = true;
let IsDelete: boolean = false;
if(IsActive)
{
60
console.log("You are in the If block of isActive");
}
else
{
console.log("You are in the else block of isActive");
}
if(IsDelete)
{
console.log("You are in the If block of isDelete");
}
else
{
console.log("You are in the else block of isDelete");
}
3) if-else - if the statement: If-else-if statement executes based on the multiple conditions. In this
statement, we can have 'if' and ''else', also we can have use multiple 'else-if' clause. As soon as the
condition meets it will execute that conditional block & rest gets ignored. If none of the condition executes
then it will execute the else block.
Syntax:
if (expression_1)
statement1;
else if(expression_2)
statement2;
else if (expression_3)
statement3;
else
statement4;
Example: Consider the below example, we are going to use if-else-if statements into it.
61
else if (strLength <=3 && strLength>2) {
console.log("string length is three.");
}
else if (strLength <=4 && strLength>3) {
console.log("string length is four.");
}
else{
console.log("string length is greater than four.");
}
Summary: In this chapter, we covered variables, naming conventions, why to use variables, variable
declaration, Re-initializing / re-declaring of variables, blocked scope, use of let & const, Operator & its
types in TypeScript. In the next chapter, I will cover enum & types of enum in TypeScript.
62
Chapter 5. enum in TypeScript
Introduction:
We have already learned different data types in the previous chapter. In this chapter we will learn what
an enum is, advantages of the enum, types of the enum, reverse mapping, function & interface using
enum, const enum, map enum & export enum in TypeScript. In this chapter, we will cover the following:
1. What is enum?
2. Advantages of an enum?
3. Types of enum
4. Reverse mapping
5. Function & interface using enum
6. const enum
7. map enum
8. export enum
The short form of enumeration is enum. It is used as a type with some set of values called a
named constant. Internally it holds numeric type values or in some cases, we can create
heterogeneous enums. The enum keyword is used to define the enums. We can create a distinct
case so it makes it easy to document.
It forces us to use all possible values. Access the enums using an index or use dot operator after
the enum name & named constant. The initial value starts from 0 if we do not set any value. Auto
incremental is the best approach to use because we do not need to worry about the values
Syntax:
enum enum_Name {
constant1, constant2, ... constantN
}
Example:
63
Consider the below example, WeekDays are declared as an enum & for Wednesday we are assigning the
value as 10 so the next value will be increasingly based on that.
enum WeekDays
{
Monday,
Tuesday,
Wednesday = 10,
Thursday,
Friday,
Saturday,
Sunday
}
2. Advantages of Enums:
The use of enums makes code more readable and manageable. Enums are a set of named constants.
Always starts from 0 if the value is not set. Supports numeric, string & heterogeneous enums.
3. Types of enums:
I) Numeric enum
I) Numeric enum:
Here enums are numeric based enums, having numeric values in them. The enum keyword is used to
declare an enum. Consider the below example having cables like mobile, TV, SetTop box, Landline phone,
etc.
64
Example:
65
C) Passing a function in enums:
Scenario One:
Consider the below example. We can pass a function to enum which returns a numeric value as:
function calculateLength(): number{
return 100;
}
enum Cables
{
Mobile = 20,
TV = calculateLength(),
SetTopBox = 25,
LanlinePhone = 34
}
console.log("value of enum Mobile is: ",Cables.Mobile);
console.log("value of enum TV is: ",Cables.TV);
console.log("value of enum SetTopBox is: ",Cables.SetTopBox);
4. Reverse Mapping: We can access the value of enum from their value directly.
enum Employee
{
Name = "Sam",
Id = 1010,
Is_Active = 20,
Is_Delete,
DeptId
66
}
console.log("value of enum Name is: ",Employee.Name);
console.log("value of enum Name is: ",Employee["Name"]);
console.log("value of enum Is_Active is: ", Employee[20]);
We are going to learn Interface in detail in the next chapter, here we will just consider the below example
only for reference purposes. clothSize is an enum which contains some constants. IClothSize is an interface
that contains two properties, key & value, having string & number type respectively. The function is taking
enum as a parameter & will print the data on the console.
enum clothSize {
small,
medium,
large
}
interface IClothSize {
key: string,
value: number
}
67
case clothSize.large:
return {
key: clothSize[clothSize.large], value: 30
};
}
}
console.log("the cloth is " + getClothSize(clothSize.small).key + " & the
value is " + getClothSize(clothSize.small).value);
console.log("the cloth is " + getClothSize(clothSize.medium).key + " & the
value is " + getClothSize(clothSize.medium).value);
6. const enum
Typescript allows us to declare a const enum using a const keyword. It has inlined values. We can access
enum using enum['ConstantName']
Example:
const enum myColor {
Red = 10,
White = Red * 4,
Blue = White + 10,
Yellow,
}
console.log(myColor.Red)
console.log(myColor.White)
console.log(myColor.Blue)
console.log(myColor['Yellow'])
Execute the above code & we will get this output:
10
40
50
51
We will not be able to access enum using index in const enum like myColor[0], it will give us an error.
68
Example:
enum classes {
I,
II,
III,
IV,
V
}
const ClassNames = new Map < number,
string > ([
[classes.I, '100'],
[classes.II, '200'],
[classes.III, '300'],
]);
console.log(ClassNames);
We will modify the above example, we can use this into a class as below.
Note
Example
enum classes {
I,
II,
III,
IV,
V
}
const ClassNames = new Map < number,
string > ([
[classes.I, '100'],
[classes.II, '200'],
[classes.III, '300'],
]);
class AllStandards {
public allNames: object;
69
constructor() {
this.allNames = ClassNames;
}
}
let obj: AllStandards = new AllStandards();
console.log(obj.allNames);
8. export enum
TypeScript allows us to export the enum. export keyword is used before the enum. Import is used to
import that enum file. We can declare the enum as below.
To import the enum in .ts, i.e.; typescript file looks like this:
Summary: In this chapter, we covered what is an enum, advantages of the enum, types of the enum,
reverse mapping, function & interface using enum, const enum, map enum & export enum in TypeScript.
In the next chapter, I will cover loops & control statements in TypeScript.
70
Chapter 6. Loops & control statements in TypeScript
Introduction:
Many times while creating an application we need to repeat some conditions. We can use loops, and we
can control these loops using break, continuous control statement, iterators & generator in TypeScript. In
this chapter we will learn what a loop is, why to use loops, real-life examples, types of loops & how we
can control the loops. In this chapter, we will cover the following
A loop in a computer program is an instruction that repeats until a specified condition is reached.
The purpose of loops is to repeat the same, or similar, code several times. Because we want to repeat
something: like Count from 1 to 100. This number of times could be specified to a certain number
depending upon the conditions, or the number of times could be dictated by a certain condition being
met.
3. Real-life Example:
A) For each customer that has an outstanding balance, send out an email reminder that payment is
due.
B) For each directory under this one, find music files and add them to the list of known music.
71
4. for loop: for loop executes the code block for a specified number of times. It is executed based
on the conditions on a fixed set of values. We need to pass a starting point, termination condition
to this loop.
Syntax:
Example:
A) Consider the below example if the user would like to print the numbers less than 10.
let i: number;
let result: string = "Output is : ";
for(i = 0;i < 10;i++) {
result = result + i + " ";
}
console.log(result)
Output is : 0 1 2 3 4 5 6 7 8 9
Example:
B) We can use for loop: Consider the example below. We will create two variables & use two for loops.
The second for loop will print the value of j and will execute only three times as our termination condition
is 3.
let i: number = 1;
let j: number = 1;
for (i; i <= 5; i++){
for (j; j <= 3; j++){
console.log(j);
}
}
72
Execute the above code & we will get this output:
1
2
3
Example:
C) Use of if condition: We can use if conditions based on our logic like we would like to exit the loop if the
inner for loop condition is satisfied.
let i: number = 1;
let j: number = 1;
for (i; i <= 5; i++){
for (j; j <= 3; j++){
console.log("j variable : "+ j);
}
if (j <= i)
{
break;
}
else
{
console.log("i variable : "+ i);
}
}
5. for..of: for iterating over iterable collections. The objects that have a Symbol.iterator property.
73
Example: In the below example, we have defined the array having some values & will print it using for..of
in console
1001
Sam
true
false
Example: Consider the below example which contains an employee object having some properties like
firstName, lastName, etc & we will access using the for-in loop:
firstName: Sam
lastName: Joy
address1: Street Road, IND
pinCode: 411010
74
7. while loop: while loop is iterating through a block of code while a specified condition is true.
While condition becomes false the loop will terminate.
Syntax:
while (condition) {
// statements to be executed
}
let i: number = 0;
let result: string = "Output is : ";
while (i < 10) {
result = result + i + " ";
i++;
}
console.log(result);
Output is : 0 1 2 3 4 5 6 7 8 9
B) While loop using array: We can use array and execute the while loop:
75
8. do-while: This is the same as while loop, the condition will check at the end of the loop; i.e., the
loop will always be executed at least once, even if the condition is false.
Syntax:
do {
// statements to be executed
}
while (condition);
Example:
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Suppose we change the while condition to while(i < 1) then the loop will execute one time at least due
to the fact that the while condition is at the end in the do while statement.
76
The number is 0
In loops we need to have control over our loops while using them, so break & continue are the best way.
A) Break: The break statement jumps out of a loop. Consider in the below example we will add if condition,
so if the condition is true then it will break the loop.
Example:
let i: number = 0;
let result: string = "Output is : ";
while (i < 10) {
result = result + i + " ";
i++;
if (i == 5) {
break;
}
}
console.log(result);
Output is : 0 1 2 3 4
B) Continue: The continue statement jumps over one iteration in the loop. Consider the below example
in which we will add continue in the if condition, so if the condition is true then the loop will continue.
Example:
let i: number = 0;
let result: string = "Output is : ";
for (i = 0; i < 6; i++) {
result = result + i + " ";
if (i === 4) {
continue;
}
}
console.log(result);
77
Execute the above code & we will get this output:
Output is : 0 1 2 3 4 5
What is an Iterator?
Syntax:
interface Iterator<T> {
next(value?: any): IteratorResult<T>;
return?(value?: any): IteratorResult<T>;
throw?(e?: any): IteratorResult<T>;
}
Symbol.iterator property is already implemented in data types like Array, Map, Set, String, Int32Array,
Uint32Array, etc.
Example:
1) Using for..of is used as like, consider the below example having an array & it will print the elements in
the array using for..of.
78
Execute the above code & we will get this output:
Sam
John
1010
true
2) Using Iterator: Consider the below example of iterator, will create a function & will pass the starting
index,
const rangeIterator = {
next: function() {
let result;
if (nextIndexValue < end) {
result = { value: nextIndexValue, done: false }
nextIndexValue += steps;
iterationCount++;
return result;
}
return { value: iterationCount, done: true }
}
};
return rangeIterator;
}
79
AsyncIterator: It returns a Promise for the iteration result, rather than the result itself. Consider below
syntax, an object which implements the interface.
Syntax:
interface AsyncIterator<T> {
next(value?: any): Promise<IteratorResult<T>>;
return?(value?: any): Promise<IteratorResult<T>>;
throw?(e?: any): Promise<IteratorResult<T>>;
}
Example: Consider the below example, we have created an array of numbers having some values
assigned at the time of initialization. Here asyncIterator() is used & created as an object of this function.
The function will check the length of the array & return a promise with myValue& result.
function asyncIterator() {
let myNumbers = [100, 200, 300];
return {
next: function() {
if (myNumbers.length && myNumbers.length > 0) {
return Promise.resolve({
myValue: myNumbers.shift(),
result: false
});
} else {
return Promise.resolve({
result: true
});
}
}
};
}
(async function() {
await iterator.next().then(console.log);
await iterator.next().then(console.log);
await iterator.next().then(console.log);
})();
80
What are Generators?
We can create a generator function using function *. It returns a generator object. It follows the iterator
interface (i.e. the next, return and throw functions).
In previous Typescript versions, we could not identify the value of yield or return from the generator, so
Generator type is introduced. The generator easily identifies the value from the iterator ; this is the main
advantage.
Syntax:
Declare a generator function using asterisk after the function keyword & generator name as follows
function* myGenerator() {
// statement block here...
}
What is Yield's expression?
Use yield keyword to send a value back to the caller in the statement blocks. This creates a lazy iterator.
The function body does not execute the next statement unless and until we call next() & only a single
function will resume at a time. The function will pause to execute successfully.
Yield allows pausing its communication. It also helps to pass control to an external system. We can push
the value from the external system into the generator function body. The external system can throw an
exception to the generator function body. A yield & yield * expression has data type any. yield * expression
is always assignable to iterable<any>.
Example:
function* generator(){
console.log('Execution started');
yield 0;
console.log('Execution resumed');
yield 1;
console.log('Execution resumed');
}
81
var iterator = generator();
console.log('Starting iteration');
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
Summary: In this chapter, we covered what a loop is, why use loops, real-life examples, types of loops,
how we can control the loops, Iterator & generator in TypeScript. In the next chapter, I will cover Function
in TypeScript.
82
Chapter 7. Function in TypeScript
Introduction:
While creating large applications we need to write the functions which will be re-used; anyone can extend
the functionality. In this chapter we will learn what a function is, Named function, Anonymous Function,
Function Type, Inferring the Types, Default parameter, Optional parameter, Rest parameter & Function
overloading in TypeScript programming language. In this chapter, we will cover the following
These are the main building blocks of any programming language or application. Use these to build a
strong application, hide complexity, abstract the information, and hide valuable information from an
outsider. Functions are used to do some things / perform the operations easily. We can create a named
function or anonymous function in Typescript. It’s easy to build our application by using different
approaches & easy to understand as well.
2. Named function
These can be declared using a named identifier, so this can be used to share in other functions as well.
These also help in the reusability of code. Users can pass parameters & it returns a data/data type value
based on conditions.
Syntax:
83
Here the function keyword is used. function_Name is the function name used to identify it. The
parameter list should be comma-separated. return_type is a data type that will be returned by the
function. A statement block is the number of states that would like to execute.
Example:
Consider the example below, which returns a full name of a person. We are passing first name & last
name to our function.
3. Anonymous Function:
These can be used as an inline function. Users can use these functions one-time using function names.
Example:
15
4. Function Type:
In function, there are two main important pillars like the type of the arguments & return type. The function
may or may not return the value, it depends upon our requirement.
Example 1) Consider the below example, we are passing string parameters as arguments to our function
& function returns a combined string value.
84
//we can write the in different way like
let firstName: string, lastName: string;
function getFullName(firstName, lastName) {
return firstName + " " +lastName;
}
console.log(getFullName("John","Kent"))
John Kent
Example 2) Consider the below example, we are passing string parameters to our function & we are just
printing a message on console. We will get the same result as above.
John Kent
15
The TypeScript compiler will take care of the type even if we used types at one side. No need to worry
about the types while using it. This will save the programmer typing effort. If we used such types, then
this is called “contextual typing".
85
console.log(getAddition(10, 20));
30
50
6. Default Parameter:
No need to maintain the sequence of parameters. When the user uses the function at that time, the user
must pass the parameter values.
Example:
a: 10 & b: 20
Example: Consider the below example, we can pass the parameter to the number type as number, null
& undefined.
function getValues(a: number, b: string){
console.log("a: " + a + " & "+ "b: " + b);
}
getValues(10, "Welcome!!!");
getValues(null, "Welcome!!!");
getValues(undefined, "Welcome!!!");
a: 10 & b: Welcome!!!
a: null & b: Welcome!!!
a: undefined & b: Welcome!!!
86
7. Optional Parameter:
Users can declare optional parameters using '?'. This means users don’t need to pass a value to the
parameter. Users can declare it after the required parameter.
Example:
a: 10 & b: 20
a: 1 & b: undefined
Example: Consider the below example, we need to take care about the sequence of parameters as well.
Will get the error ‘An argument for 'b' was not provided’. We must have to follow the sequence; i.e.
optional parameter must be declared after the required parameter.
8. Rest Parameter:
Typescript allows users to use rest parameters to use n number of parameters used. Users can use these
when they are not sure about the parameters list or unknown parameters. Rest parameters can be
denoted by an ellipsis (...) This should be used at the last parameter in the parameter list.
Syntax:
87
Example: Consider the below example, we will create one function & pass rest parameters to this
function as a number of the array.
Example: Users can pass the rest parameter as any typed array so the user can pass any data type values
like a number, string, Boolean, etc.
Example: Rest parameter must be used at the last parameter in the parameter list otherwise it will give
us errors:
9. Function Overloading:
88
Typescript allows us to create function overloading. The function name should be the same with different
parameters & return types. We can change the sequence of the parameter as well.
Example: Consider the below example, getAddition is a function taking two parameters of any data type
& return type as well.
300
Welcome to CoderFunda.com !!!
Summary: In this chapter, we covered what a function is, Named function, Anonymous Function,
Function Type, Inferring the Types, Default parameter, Optional parameter, Rest parameter & Function
overloading in TypeScript. In the next chapter, I will cover the Interface in TypeScript.
89
90
Chapter 8. Interface in TypeScript
Introduction:
TypeScript supports object-oriented principles like Classes, Object, Interface, Polymorphism, etc. Object-
oriented principles are useful to reuse the code in the application & the developers who are familiar with
the OOPS concept can easily learn TypeScript. In this chapter, we will learn what Interface is and Types
of Interfaces. In this chapter, we will cover the following
1. What is Interface?
2. Implementation of an Interface
3. Types of Interface
4. Read-Only properties in Interface
5. Optional Property in Interface
1. What is Interface?
An interface is contract specific. It acts as an interaction between two entities. The interface is a
description of the function that a class is obliged to do. In interfaces, we can define properties, methods,
and events. The properties, methods & events are the members of the interface.
We can only declare the members. Methods are implemented later after the implementation of the
interface.
Real-life example:
Suppose there is a function to process data, two entities like files stored on the local drive of a computer
& files stored in the database. Both the entities are processing a file based on the condition at runtime,
so we need an interface that is implemented by both the processors.
91
Important points
● All methods in the interface much have to implement at the time of interface implementation
● The class can implement n number of interfaces at a time.
● It does not have a constructor.
Syntax
interface interfaceName {
// declaration of properties, function names
}
Example:
interface IEmployee {
employeeId: number;
employeeName: string;
}
1000, John
1001, Sam
92
2. Implementation of an Interface
Consider the below example, in programming languages the OOPS concept has an interface and it is
implemented using the implements keywords as below, IPerson is an interface & it’s implemented in
Person class.
The implemented class must have implemented it with the properties & functions with the same data
type defined as in the interface. There are properties in an interface like person id, person name & person
address. getAddress is a function that takes a numeric parameter & returns a string.
Example
interface IPerson {
personId: number;
personName: string;
personAddress: string;
getAddress:(x: number)=>string;
}
getAddress(empCode: number):string {
return this.personAddress;
}
}
3. Types of Interface
A. Function Types
93
Typescript allows us to define the function in Interface & interface is used to define a type of a function.
Consider the below example, we have defined some properties & functions in interface & we are accessing
it.
Example:
interface IEmployee {
employeeId: number;
employeeName: string;
getDetails(): any;
}
function getDetails()
{
return "Hello !!!";
}
let emp1: IEmployee = { employeeId: 1000, employeeName: "John"};
let objForGet: IEmployee = getDetails;
console.log(objForGet());
console.log(emp1.employeeId + ", " + emp1.employeeName );
Consider the below example, Interface contains only the signature of a function having a single
parameter & returns a string value. Now we will define the function & will assign that function to the
interface variable. While accessing the variable we will pass the parameter.
interface IEmployee {
(employeeId: number): any;
}
function getDetails(employeeId: any)
{
return "Hello !!!" + " & employee id is: " + employeeId;
}
let emp1: IEmployee = getDetails;
console.log(emp1(1001));
94
B. Indexable Types
We can define the Interfaces as an Indexable Type & it has an Indexable signature. Users can access it
via Indexes.
let a: object;
a = { x: 1, y: 'true', z: 'Sam'};
console.log(a['x']);
console.log(a['y']);
console.log(a['z']);
Now consider the below example. We will define one Interface IEmployee which contains Indexes & is
an object to which we are assigning the values in Indexes. Now we will access the values using Indexes.
interface IEnployees {
[Employees: number]: string;//indexer
}
Also we can define the other members & function in above interface as well:
interface IEnployees {
[Employees: number]: string;
isActive: boolean;
getEmployeeAddress(): string;
}
IEmployee is an interface that has an index signature. IEmployee is indexed with a number & that will
return a string. IEmployee also contains property isActive as boolean& a function getEmployeeAddress
will return a string.
95
C. Class Types
Class is a collection of objects. It contains properties and methods. Consider class Person having different
properties like personName, personAge, PersonAddress & methods like getPersonDetails().
It is the blueprint of data & behavior, which means how the data would look & how the functions work.
Real-life example
Consider an example. If a person would like to build a house then he first needs to draw a pictorial
representation of this house & some functions like openDoor, useOfLift, etc.
Syntax:
class ClassName{
// properties
// behaviour
}
What is a Constructor?
It is used to initialize the members of the class. The constructor keyword is used to define the
constructor.
96
Example:
class Person{
id: number;
constructor(i: number) {
this.id = i;
}
}
let p: Person = new Person(100);
console.log(p)
Execute the above code & we will get this output:
Person {id: 100}
Example:
Consider the below example. interface IEmployee contains some properties & we will declare one class,
Employee, which will implement the IEmployee interface. The constructor of class will assign the values
to properties.
interface IEnployees {
employeeId: number;
employeeName: string;
isActive: boolean;
}
class Employee implements IEnployees{
eId: number;
eName: string;
eActive: boolean;
constructor (eId: number, eName: string, eActive: boolean) {
this.employeeId = eId;
this.employeeName = eName;
this.isActive = eActive;
}
}
let objEmployee: Employee = new Employee(1001, "Sam Sinha", true);
console.log(objEmployee);
D. Hybrid Types
While creating an application we need to create an object that will act as a function and an object. In
some third-party applications, we need to implement such kinds of scenarios.
97
Consider the below example. We have defined two interfaces, and one function. For the object of
INewSearch interface, it will pass the parameters to functions & if string search finds the first substring
match in a regular expression search then it returns a boolean value.
interface ISearchFunction {
(a: string, b: string): boolean;
}
E. Extend Interface
As we know we can extend the classes, so interfaces also can be extended to each other. Users can copy
the members of one interface into another by declaring multiple interfaces. It helps the user to reuse
components.
Example:
Consider the below example, we have defined the two interfaces IDepartment & IPerson having some
properties & will extend IDepartment into aIPersoninterace. So while creating an object of IPerson we will
be able to access the property of IDepartment interface.
interface IDepartment {
deptId: number;
}
98
interface IPerson extends IDepartment {
personId: number;
}
console.log(person);
interface ISector {
sectorName: string;
}
interface IDepartment {
deptId: number;
}
console.log(person);
console.log(person1);
99
Read-Only property can’t be changed once it is initialized. When the user would like to use some fixed
values to the property in this case they can use read-only.
interface Circle {
readonly pieValue: number;
getString(): string;
}
function getString(): string{
return "Hello !!!"
}
In the interface, we can define the optional properties. So, we need to assign the value.
Consider the below example, we will add one property; i.e. personality, which will be optional. So, no
need to assign the value in the constructor.
interface IPerson {
personId: number;
personName: string;
personAddress: string;
personSalary?: number;
getAddress:(x: number)=>string;
}
getAddress(empCode:number):string {
return this.personAddress;
}
}
100
console.log("Name: " + objPerson.personName);
console.log("Address: " + objPerson.getAddress(objPerson.personId));
Summary: In this chapter, we covered the major concepts of Object-oriented programming language
which are supported by Typescript; i.e., Interfaces, Types of Interfaces & property declarations in Interface
in TypeScript. In the next chapter, I will cover Classes & some object-oriented concepts in TypeScript.
101
Chapter 9. Classes in TypeScript
Introduction:
TypeScript supports OOPS concepts so we can easily define the Classes, Objects, Constructors, Access
Modifiers, Accessors, Inheritance, Types of Inheritance, Polymorphism, Property Declarations in
TypeScript. Class is the building block of every application that contains properties, methods, how we
inherit the class, how to override the methods, how to use access modifiers, etc. In this chapter, we will
cover the following
1. Classes
2. Objects
3. Constructors
4. Access Modifiers
5. Accessors
6. Inheritance
7. Type Of Inheritance
8. Polymorphism
9. Property Declarations
1. Classes
Real-life example
1) Light bulb
A light bulb has some characteristics like the coil, cover, below surface etc., and some behavior like
bulb ON / OFF.
2) Construct a house
102
Consider an example, If a person would like to build a house then he first needs to draw a pictorial
presentation of this house & some functions like openDoor, useOfLift, etc.
Syntax:
class ClassName{
// properties
// behaviour
}
Example: Consider the below example. Person is a class containing id property and a constructor &
personDetails is a function/method which returns an id value.
class Person{
id: number;
constructor(i: number) {
this.id = i;
}
personDetails()
{
return this.id;
}
}
2. Objects
The object is an instance of the class. Objects have state & behavior. Users can create an instance of a
class using a new keyword as well. Consider in real life: A human is an object which has states like name,
103
age, color, etc & behavior like walk, talk, speak, etc. object (lowercase) is used for non-primitive types &
Object (uppercase) used for functionality which is common to use.
interface Object {
personId: number;
personName: string;
personAddress: string;
getAddress:(x: number)=>string;
}
Empty Type:
It is used to declare when no member is inside it.
let obj = {}
When we are trying to assign any value to empty type then the compiler will give us an error like
Property 'id' does not exist on type '{}'
Example:
let obj = {}
obj.id = 100;
104
Example: Consider the below example. Person is a class which contains property id & constructor. We
have created an object of Person class using the new keyword.
class Person{
id: number;
constructor(i: number) {
this.id = i;
}
}
let p: Person = new Person(100);
Consider the below example, the employee object contains some properties & we can access these
using (dot) . Operator like
let employee = {
id: 1000,
name:"John Kent",
address:"US"
};
console.log(employee.id)
console.log(employee.name)
console.log(employee.address)
Consider the below example we can access the element using Indexable type like
Example;
let a: object;
a = { x: 1, y: 'true', z: 'Sam'};
console.log(a['x']);
console.log(a['y']);
console.log(a['z']);
105
1
true
Sam
Consider that the below example is an object literal. Function myFunction is taking an object as a
parameter while calling the function will pass the employee object.
Example:
let employee = {
id: 1000,
name:"John Kent",
address:"US"
};
let myFunction = function(objectType: { id:number, name: string, address :string }) : any {
console.log(objectType.id)
console.log(objectType.name)
console.log(objectType.address)
}
myFunction(employee)
3. Constructor
What is a Constructor?
It is used to initialize the members of the class. The constructor keyword is used to define the constructor.
Example:
1) Consider the below example
class Person{
id: number;
constructor(i: number) {
106
this.id = i;
}
}
let p: Person = new Person(100);
console.log(p)
2) Consider the below example. Interface contains a method, class, which is having some properties.
The constructor will assign values to these properties & after the creation of the object it will print the
values into a console.
interface IPerson {
getAddress:(x: number)=>string;
}
class Person implements IPerson {
personId: number;
personName: string;
personAddress: string;
constructor(id: number, name: string, address: string) {
this.personId = id;
this.personName = name;
this.personAddress = address;
}
getAddress(empCode:number):string {
return this.personAddress;
}
}
let objPerson = new Person(1001, "Sam","M G Road, Delhi");
console.log("Id: " + objPerson.personId);
console.log("Name: " + objPerson.personName);
console.log("Address: " +objPerson.getAddress(objPerson.personId));
Constructor function: Below is the construction function for the person object
Example:
107
this.PinCode = pinCode;
}
var firstPerson = new Person("John", "Pune", 41101,);
console.log(firstPerson.Name);
console.log(firstPerson.Address);
console.log(firstPerson.PinCode);
Output:
John
Pune
41001
Built-in constructors: TypeScript has built-in constructors for native objects like below.
Example:
console.log(A);
console.log(B);
console.log(C);
console.log(D);
console.log(E);
Output:
{}
String {""}
Number {0}
Boolean {false}
[]
4. Access Modifiers
Typescript supports access modifiers which is Encapsulation. It is used to make members
accessible in the class or outside the class. Below are the access modifiers.
108
1) Public:
By default, all class members are public in Typescript. These members can be accessible
anywhere.
Example:
class Person{
public Id: number;
public Name: string;
Address: string;
constructor(id:number, name: string, address: string)
{
this.Id = id;
this.Name = name;
this.Address = address;
}
}
let obj = new Person(120, "Sam","Pune");
console.log(obj.Id)
console.log(obj.Name)
console.log(obj.Address)
2) Private:
These members are only accessible inside the class. Outside the class, these are not accessible.
3) Protected:
These members are accessible only to the derived type.
Example:
109
class Person{
public Id: number;
private Name: string;
protected Address: string;
constructor(id:number, name: string, address: string)
{
this.Id = id;
this.Name = name;
this.Address = address;
}
}
let obj = new Person(120, "Sam","Pune");
console.log(obj.Id)
console.log(obj.Name)
console.log(obj.Address)
Will get an error when we try to access the Name & Address as:
Property 'Name' is private and only accessible within class 'Person'
Property 'Address' is protected and only accessible within class 'Person' and its subclasses.
We will create one more class as Details and extend the Person class. In detail, the class will write
one method which will print the base class member values. We are not able to access the private members
in the derived class.
class Person{
public Id: number;
private Name: string;
protected Address: string;
public constructor(id: number, name: string, address: string)
{
this.Id = id;
this.Name = name;
this.Address = address;
}
}
class Details extends Person{
constructor(a: number,b: string, c: string){
super(a,b,c);
}
110
getData(){
console.log(this.Id);
//console.log(this.Name);
console.log(this.Address);
}
}
let obj = new Details(120,"Sam","Pune")
obj.getData();
120
Pune
5. Accessors
Typescript allows us to get & set the members of the value inside the class. getter & setter are
two methods
1) Getter: Will retrieve the value of the member. get keyword is used to get the value.
Syntax:
get PropertyName()
{
// code
}
Example:
class Person {
id: number;
111
private name: string;
protected address: string;
constructor(i: number, n: string, a: string){
this.id = i;
this.name = n;
this.address = a;
}
get getDetails(): any {
return this.id + " " + this.name + " " + this.address;
}
}
let obj = new Person(1001, "Sam","Pune")
console.log(obj.getDetails);
Output
class Person {
id: number = 1002;
private name: string ="Sam";
protected address: string = "Pune";
Output
2) Setter: We can set the value to members using the set keyword.
Syntax
112
set PropertyName()
{
pName = value
}
Example:
class Person {
name: string;
Output:
Sam
6. Inheritance
TypeScript allows us to use the concept of Inheritance. Users can create new classes from the
existing class. The existing class can be called the base class & the new class can be called a
derived class. By using 'extends' keyword users can inherit the base class; i.e. all properties and
methods will be accessible in derived class & constructors from the base class. In Inheritance
users can access the base class properties as well as methods, if the user would like to add more
functionality to that base method then he can easily modify or extend the methods.
Real-life example:
Consider a real-life example. In the old days there was a watch used to identify the time, so
watches had some functions like calculate hours, minutes, etc. Now the same methods &
properties are used in a smartwatch with some additional functionality, here Inheritance is used.
113
Why Inheritance:
It is used to code reusability which means we can add or remove the existing functionality easily. We can
override the base methods without changing the parent class. Inheritance hierarchy represents an "is-a"
relationship and not a "has-a" relationship. We can easily make global changes to derived classes by
changing a base class.
Syntax:
Example:
class person{
id: number;
name: string;
constructor(i: number, n: string)
{
this.id = i;
this.name = n;
}
getDetails(): void {
114
console.log(this.id +" , " + this.name)
}
}
class personDetails extends person
{
let obj = new person(100, "Sam");
obj.getDetails();
}
Output:
100, Sam
7. Types of Inheritance
1) Single Level Inheritance: In which one class can be extended into another class.
Consider the below example, a base class person having properties & one constructor to initialize these
all properties. We will extend the person class into personDetails class & create an object of the base
class by passing parameter values.
class person{
public id: number;
private name: string;
protected address: string;
constructor(i: number, n: string, a: string)
{
this.id = i;
this.name = n;
this.address = a;
}
getDetails():void {
console.log(this.id +" , " + this.name +" , " + this.address)
}
}
class personDetails extends person
{
let obj = new person(100, "Sam","Pune");
obj.getDetails();
115
}
Output:
2) Multi Level Inheritance: In which we can extend more than two levels of Inheritance. In the last
derived class, we can access the elements of first base class & we can initialize all.
class person{
id: number = 0;
name: string = "";
address: string = "";
getDetails():void {
console.log(this.id +" , " + this.name +" , " + this.address)
}
}
class personDetails extends person
{
}
Output:
116
8. Polymorphism
Poly means single name & morphism means many forms. It is one of the important object-
oriented concepts. It means function has the same name & multiple meanings. It means the name
of the function is the same but its definitions are different; i.e. function overloading. There are
different rules for the naming declaration used by the different compilers.
Function Overloading
It means we can overload the function using the same function name having a different
parameters list. Function overloading means function redefinition. The return type should be
different.
Example:
In the below example, function overloading is used, doAdditiontakes two parameters &
returns any data type value. First function is taking two string parameters & the second
function is taking two numeric parameters.
Output:
30
We can’t achieve the function overloading by fewer parameter lists or more parameter lists as:
117
function doAddition(a: string, b: string): any;
function doAddition(a: string, b: string, c: number): number;
function doAddition(a: any, b: any): any {
return a + b;
}
console.log(doAddition("welcome", " to Typescript !!!"));
console.log(doAddition(10));
console.log(doAddition(10, 20, 30));
Function Override:
Function override means the base class function can be overridden in the derived class. The
function call depends upon the object creation of the class.
Example:
class baseClass{
doTask(): void{
console.log("Test")
}
}
class derivedClass extends baseClass{
doTask(): void{
console.log("Hello");
}
}
Output:
Hello
Test
118
Typescript allows us to define the optional property in the class. By using the '?' symbol we can define the
optional property. It means there’s no need to assign the value to that property, it is optional. Many times
users aren't aware of the value.
Syntax:
propertyName? : datatype;
Example: Consider the below example, the name is an optional property. In the constructor, we have
initialized the class members.
When we create an object at that time, we will decide whether to pass a value or not. For first object
creation we are not passing name value to Person constructor, in second object creation we are passing
name value to Person constructor.
class Person{
id: number;
name?: string;
constructor(i: number, n?: string) {
this.id = i
this.name = n;
}
}
let obj: Person = new Person(1000);
console.log(obj.id)
console.log(obj.name)
The difference between optional parameter & parameter type is we need to supply value in parameter
type.
119
return a;
}
function getValueWithoutOptional(a: number | undefined) {
return a;
}
getValueWithOptional(); // works fine
getValueWithoutOptional(1); // works fine
getValueWithoutOptional(); // this function expect at least one parameter
B. Readonly Properties
Typescript allows us to declare the property as readonly. The 'readonly' keyword is used to
declare the property. When we create the first object at that time we can assign the value. After
that, we can’t modify the property.
Syntax:
Example:
In the below example, we have defined the read-only property, while creating the object we are assigning
the value to it. After that we can't modify the value and will get an error like "Cannot assign to 'id' because
it is a read-only property."
class Person{
readonly id: number;
constructor(i: number) {
this.id = i
}
}
let obj: Person = new Person(1000);
console.log(obj.id)
obj.id = 2000;
C. Static Properties
The static keyword is used to define static property. Static members of the class are accessible without
creating an object of the class. We can directly use static members by using. (dot) operator. We can
reinitialize the values of static members.
120
Syntax:
Example:
class Person{
static id: number;
}
Person.id = 1000;
console.log(Person.id)
Person.id = 2000;
console.log(Person.id)
Summary: In this chapter, we covered major concepts of Object-oriented programming language which
are supported by Typescript like Classes, Objects, Constructors, Access Modifiers, Accessors, Inheritance,
Types of Inheritance, Polymorphism, Property Declarations in TypeScript. In the next chapter, I will cover
Generics in TypeScript.
121
Chapter 10. Generics in TypeScript
Introduction:
While creating large applications, for functions we need to pass the parameter, which has different types
& it should be type-safe. These parameters will sometimes have a single value or multiple values; or
sometimes will have different values with different data types, So according to that, we need to build the
functions. TypeScript supports generic class, generic function, generic interface, etc. In this chapter, we
will cover the following
Object-oriented programming is used to build in such a way that users can easily reuse the code.
While creating an application we need to repeat the code to handle different conditions so we
can create a generalized method. Generics have a feature to create reusable components.
Typescript allows us to use it in function, interfaces & classes as well. Consider the below
examples, suppose we have a function which returns a number & boolean types values.
Now above both of the functions are doing the same job.
122
The identity function is a function which accepts a specific type & returns an argument.
Example:
Case 1:
Output
1000
Case 2:
Suppose we would try to pass a string parameter to the above function then we will get an error
like Argument of type '"hello"' is not assignable to parameter of type 'number'.
Case 3:
Typescript allows us to create a function using identity() which will take a type of parameter &
return an argument.
123
This is not type-safe, so make a type-safe use type of variable. This is used when the user must
pass multiple parameters to a function.
Here T is used for Type. To pass a generic type we use angled brackets <> after the name of our
function.
Users can pass the multiple generic types to a function & return type T remains the same as it is.
Example: In the below identity function we are passing different types of parameters.
3. Key-Value Pair:
124
Consider the below example, assignValues is a generic function that takes different types of
parameters. The class contains key &val properties that are initialized in function.
4. Generic Type
Non generic functions would be declared by passing the parameter argument which will be
returned from the identity function.
10
Hello Word
125
Hello
In the above section, we have checked how identity function takes a different type of parameter.
Now suppose we would like to print the length on the console; then we will use arg.length
For number data type we will get an error "Property 'length' does not exist on type 'T'" if we
calculate the length.
Now, we will modify the argument type as array & will get the length of the argument.
Example:
Output:
Identity function returns an argument as:
4
(4) ["Sam", "John", 1010, true]
126
If we try to pass a number to the above function as argument then we will get the error
"Argument of type '1001' is not assignable to parameter of type 'unknown[]'"
Typescript allows us to use the built-in functions of properties. Users can pass the parameter to
the function & use the built-in functions in generic type functions. For function, the user can pass
any type of parameter.
Example:
console.log(worldString.blink());
console.log(worldString.bold());
console.log(worldString.italics());
console.log(worldString.strike());
console.log(worldString.lastIndexOf("world"));
<blink>Hello, world</blink>
<b>Hello, world</b>
127
<i>Hello, world</i>
<strike>Hello, world</strike>
7
2000
Hi !!!
the id is of number & name is of string type
In the above code for generic function we will get error for the below code:
console.log(name.toFixed());
console.log(name.toUpperCase());
}
getData(2000, "Hi !!!");
getData("Welcome to Typescript", true);
getData(1001, 2002);
128
6. Generic Class
Typescript allows us to create a generic class. Users can pass the parameters in the angular
brackets. The class contains properties & methods. Specific data types allow us in the class, so
the code can be reusable. Passing a parameter is type-safe & when the class is used in different
data types it will be useful.
Consider the below example, employee class is a generic class in which emp is one member & in
the constructor will assign the value to it. We will pass the array while creating an object & these
contain different data type values.
class employee<T> {
public emp: T[];
constructor(arg: T[]) {
this.emp = arg;
}
}
let obj_1 = new employee(["Sam", "John", 1010, true]);
console.log(obj_1.emp);
let obj_2 = new employee([10,20,30,40,50]);
console.log(obj_2.emp);
Consider the below example, class contains generic members agr1, agr2 & one function which
will print the values of the members. The constructor will assign the values to the members.
Generic class has parameters passed using angle brackets after the class name.
class myGeneric<T,U>
{
public arg1: T;
public arg2: U;
constructor(arg1: T, arg2: U) {
this.arg1 = arg1;
this.arg2 = arg2;
}
129
getValues():void {
console.log(`arg1 : ${this.arg1}, arg2 : ${this.arg2}`);
}
}
7. Generic Constraints:
It checks which type of argument we are passing as an argument. Without constraints, it will take
any argument. By using constraints, we can increase the number of operations. The method can
be called by the generic class.
In the above two examples, we know the types of data. Now suppose the user would like to print
the length of argument, then we will modify the class as below. Typescript will not print the value
on the console because every argument does not have a valid length.
class employee<T> {
public emp: T[];
constructor(arg: T[]) {
this.emp = arg;
}
getData<T>(arg: T[]) {
console.log(this.T.length);
}
}
let obj_1 = new employee(["Sam", "John", 1010, true]);
obj_1.getData(obj_1.emp);
let obj_2 = new employee([10,20,30,40,50]);
obj_2.getData(obj_2.emp);
130
We will modify the above code to print the length of the argument.
class employee<T> {
public emp: T[];
constructor(arg: T[]) {
this.emp = arg;
}
getData<T>(arg: T[]) {
console.log("length: ",this.emp.length);
}
}
let obj_1 = new employee(["Sam", "John", 1010, true]);
obj_1.getData(obj_1.emp);
let obj_2 = new employee([10,20,30,40,50]);
obj_2.getData(obj_2.emp);
8. Generic Interface
Interface
Typescript allows us to use a generic class user interface, we can implement an interface in the
class. Generic interface types can also access multiple generic types. Key-value pairs are also
allowed in the class & we do not need to worry about it.
Generic Interface: Consider the below interface. Identity function will pass the generic parameter
which returns the argument.
interface IGenericInterface {
<T>(arg: T): T;
}
function identity<T>(arg: T): T {
return arg;
131
}
let myId_1: IGenericInterface = identity;
console.log(myId_1(10))
console.log(myId_1("Hello Word"))
console.log(myId_1("Hello","Word"))
10
Hello Word
Hello
Consider the below example, we have an interface IEmployee having two properties & function
employeedetails extends that interface. If we try to access the length of an argument, then it will
give us an error.
interface IEmployee {
ID: number;
Name: string;
}
For the time being comment console.log(arg1.length); in employeeDetails function & execute
the above function:
132
Consider the below example. IEmployee is an interface, Class employee implements an interface.
While creating an object either will pass the type.
Summary: In this chapter, we covered what Generics are, identity function using generic type, Key-Value
Pair, Generic Type, function & properties of a generic type, Generic Class, Generic Constraints & Generic
Interface in TypeScript. In the next chapter, I will cover Advanced Types in TypeScript.
133
Chapter 11. Advanced Types in TypeScript
Introduction:
TypeScript is a superset of JavaScript, so it supports some advanced types as well. In this chapter, we will
learn about Intersection, Union, Type Guards, Nullable Types, Type Aliases & Literal Types in TypeScript.
In this chapter, we will cover the following
1. Intersection
2. Union
3. Type Guards
4. Nullable Types
5. Type Aliases
6. Literal Types
1. Intersection
It allows us to create intersection types, which means it combines multiple types into a single
type. If any one type intersects with another one then this has all the properties from both types.
It combines all the properties into one, which means all features are available in a single type. It
can be used in classes, interfaces, etc. It allows using optional properties as well.
type AB = A & B;
let test: AB;
Example: Consider the below example, having two interfaces with some properties & with an
optional property as well. To our function we will pass the intersection of these two interfaces
with property values.
interface IPerson {
personId: number;
personName: string;
personAddress: string;
134
}
interface IDepartment {
deptId?: number;
deptName: string;
deptLocation: string;
}
function getDetails(myInter: IPerson & IDepartment): void {
console.log(myInter)
}
let myInter: IPerson & IDepartment = {personId: 1001, personName: "Sam", personAddress:
"India", deptName: "Engg", deptLocation: "1st Floor"};
getDetails(myInter);
let myInter1: IPerson & IDepartment = {personId: 1001, personName: "Sam", personAddress:
"India", deptId : 2001, deptName: "Engg", deptLocation: "1st Floor"};
getDetails(myInter1);
Output:
{personId: 1001,
personName: "Sam"
personAddress: "India"
deptName: "Engg"
deptLocation: "1st Floor"}
{personId: 1001,
personName: "Sam"
personAddress: "India"
deptId: 2001
deptName: "Engg"
deptLocation: "1st Floor"}
Example: Same properties cannot be defined with different / same data types in the same type.
interface A {
name: number;
}
interface B {
name: string;
address: string
}
135
type AB = A & B;
let p: AB;
p.name = "Sam";
console.log(p.name)
Example:
interface A { a: number; }
interface B { b: string; }
interface C { c: boolean; }
interface D { d: string; }
interface X { x: A & B }
interface Y { y: C & D }
Output:
{x: {…}, y: {…}}
x: {a: 1010, b: "Sam"}
y: {c: true, d: "US"}
__proto__: Object
136
2. Union
The union is declared by using a pipe symbol / vertical bar i.e. (|) separated by each type. It
combines multiple types into one. Variables can contain one or more types. It allows defining the
optional properties as well.
Syntax
Hello world
1010
true
Syntax
Example 1)
137
{
if (typeof (message) == "string")
{
console.log("value is of string type")
}
else if (typeof (message) == "number")
{
console.log("value is of number type")
}
}
getValue("hello world");
getValue(2000);
//getValue(true); //Argument of type 'true' is not assignable to parameter of type
'string | number'
Output:
Example 2) Consider the below example function having two parameters. The first message is
taking any type & the second parameter is taking union type i.e. number or string.
Output
hello world 50
20 30
138
arg2 is a different type
Example 1)
class Employee
{
public name: string;
public id: string | number
constructor(i: any, j: any)
{
this.name = i;
this.id = j;
}
}
let obj: Employee = new Employee("Sam", 2020);
console.log(obj)
let obj1: Employee = new Employee("John", "1010");
console.log(obj1)
Output:
Employee {name: "Sam", id: 2020}
Employee {name: "John", id: "1010"}
Example 2) Consider below an example of Interface which contains some properties & type
myTypes is of union type
interface IEmployee {
empId: number;
empName: string;
empAddress?: string;
}
139
interface IDepartment {
deptId: number;
deptName: string;
}
let A: myTypes = {
empId: 1010,
empName: "Sam"
};
console.log(A);
let C: myTypes = {
empId: 1010,
empName: "Sam",
empAddress: "US"
};
console.log(C);
Output:
{empId: 1010, empName: "Sam"}
{deptId: 2020, deptName: "IT"}
{empId: 1010, empName: "Sam", empAddress: "US"}
Example 3) Consider the below example, in interface function type can be used as a union.
140
3. Type Guards
The conditional block is used to restrict the type in Typescript. It is useful when we already know
the type on which we will add the conditional blocks & do the next execution. Conditional blocks
are like if, if-else, etc conditions that we can use. It allows accessing the members inside the union
type. Operators type, in & instanceof, is used in type guards. User-defined type guards are also
used in typescript.
A) typeof: It checks the primitive types in typescript. We can add the conditions to check the
types like number, string, Boolean, etc.
Example:
Output:
value is of string type
value is of number type
Example: In the below example we are using a type assertion of a class student having property
name & class standard having property teacher name. Our function returns an object of class
141
type depending on the condition. We are passing an argument to our function which will be
conditionally used. We know the type of result.
class Student {
name: string;
constructor(i: string) {
this.name = i;
}
}
class Standard {
teacherName: string;
constructor(j: string) {
this.teacherName = j;
}
}
function getDetails(stdId: number): Student | Standard {
if (stdId === 1001) {
return new Student("Sam");
} else {
return new Standard("John Sir");
}
}
let result = getDetails(10);
if (result instanceof Student) {
console.log(result.name);
} else {
//result.name(); // will get error.
console.log(result.teacherName);
}
Output
John Sir
Sam
142
C) in: It checks if the property exists in the given context or not.
Syntax:
"string_literal" in the result expression
Here string_literal is string literal type & result is the union type. It will check for the optional or
required property and if it is true then it executes the block.
Example:
class Student {
name: string;
constructor(i: string) {
this.name = i;
}
}
class Standard {
teacherName: string;
constructor(j: string) {
this.teacherName = j;
}
}
function getDetails(stdId: number): Student | Standard {
if (stdId === 1001) {
return new Student("Sam");
} else {
return new Standard("John Sir");
}
}
let result = getDetails(10);
if ("name" in result) {
console.log(result.name);
} else if ("teacherName" in result) {
console.log(result.teacherName);
}
143
Sam
Example: Different ways to check if property exists in the given context or not.
class Student {
name: string;
constructor(i: string) {
this.name = i;
}
}
class Standard {
teacherName: string;
constructor(j: string) {
this.teacherName = j;
}
}
function getDetails(stdId: number): Student | Standard {
if (stdId === 1001) {
return new Student("Sam");
} else {
return new Standard("John Sir");
}
}
if ((result as Student).name) {
console.log(result);
} else if ((result as Standard).teacherName) {
console.log(result);
}
if ((result1 as Student).name) {
console.log(result1);
} else if ((result1 as Standard).teacherName) {
console.log(result1);
}
output:
144
Standard {teacherName: "John Sir"}
Student {name: "Sam"}
4. Nullable Types
Typescript allows us to create two types as null & undefined. These are their typed names. These
are not too useful but while creating a large application we need to check if the types are null or
undefined to handle errors. We can assign null & undefined to any type. strictNullChecks will
protect nulls and undefined. It can enable you to add a strictNullChecks flag as an option. We can
add a nullable type using union type.
Example 1)
Consider the below example, name variable is of type string. In the third line we cannot assign
null to string type.
145
Example
type Employee = {
name: string;
phoneNumber: string | undefined | null;
};
output
Example 2)
Consider the below example, function is taking one argument of type number & function expects
to return number. We can’t directly return undefined.
146
We can modify the return type of function by passing union type which is explicitly defined so
function may return number, null & undefined as:
Example 3) Consider the below example function having two-arguments, the second argument
is an option, so we may or may not pass the second argument. Null is not assignable to number
as the second argument.
147
Nullable Types using Type Guard: Nullable types are achieved using union types so we can use
type guard to use the guard properly.
Example 4) Consider the below functions. We can check the argument value as equal to null or
not and by typeof we can check the type as well.
Output
10
0
Using typeof
Example:
148
}
console.log(usingTypeof(10));
console.log(usingTypeof(null));
Output
10
0
Example:
5. Type Aliases
It creates a new name that refers to the types. It is a new name for the type. It can be used in
intersection, unions, tuples, and any other valid type in typescript.
Syntax
149
Example: Consider the below example, we have a name, and fullName is of type &
completeName refers to these types. Function is taking arguments of type completeName&
returning a string.
Output
Sam
We will modify the above code, we will check the type of argument using condition.
Output
sam
the argument is not a string
6. Literal Types
150
A. String Literal Types: ES6 allows us to use a string in a new way. We need to assign the value
to our string. It can be used with the union, type guards & type aliases. We can add the conditions
as well while creating an application.
Example:
Output
America English-US
Invalid input !!!
B. Numeric Literal Types: Typescript allows us to create numeric literal types. Consider the
below example, we have a function which will take the argument as parameter & will check
odd/even numbers using a switch case statement.
151
case 0:
console.log("The below number is even number");
return value;
case 1:
console.log("The below number is an odd number");
return value;
default:
console.log("Below is invalid input!!!");
return value;
}
}
console.log(checkNumber(2));
console.log(checkNumber(5));
console.log(checkNumber(NaN));
Output
The below number is even number
2
The below number is an odd number
5
Below is invalid input!!!
NaN
152
}
doChecks(1)
doChecks(null)
doChecks(undefined)
C. Enum Member Types: In Typescript enum member has types. We can have enum members
literal-initialized. Consider the below example, we have declared an enum for ASCII Code.
Output
ASCII Code for A is 65
ASCII Code for B is 66
We can declare the boolean literal type like the below constants having values true & false
respectively. We can declare a union type as well.
153
const FALSE: false = false;
console.log(FALSE)
Union type
Conditional based:
We can check the value based on condition as well. Consider the below function which will take
one argument & will check if the argument is a valid email or not in the function based on
condition. We will return a true or false value.
return false;
}
console.log(ValidateEmail("sam@gmail.com"));
console.log(ValidateEmail("test.com"));
E. Polymorphic Types
We can create a polymorphic type in Typescript. We can use this type in class/interfaces. F-bound
polymorphism is used when a subtype contains a class/interface. The below example contains
classes having constructors & methods which are used to do the calculation like addition,
subtraction, multiplication, division, etc.
class DoMathsCalculations {
constructor(protected arg1: number = 0) { }
154
addition(operand: number): this {
this.arg1 += operand;
return this;
}
subtraction(operand: number): this {
this.arg1 -= operand;
return this;
}
multiplication(operand: number): this {
this.arg1 *= operand;
return this;
}
division(operand: number): this {
this.arg1 /= operand;
return this;
}
}
Output
DoMathsCalculations {arg1: 12}
DoMathsCalculations {arg1: 7}
DoMathsCalculations {arg1: 42}
DoMathsCalculations {arg1: 4.2}
Summary: In this chapter, we covered some advanced types of Intersection, Union, Type Guards, Nullable
Types, Type Aliases & Literal Types in TypeScript.
References
1) www.typescriptlang.org
2) https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html
155
156