Download Full TypeScript Basics: Learn TypeScript from Scratch and Solidify Your Skills with Projects 1st Edition Nabendu Biswas PDF All Chapters
Download Full TypeScript Basics: Learn TypeScript from Scratch and Solidify Your Skills with Projects 1st Edition Nabendu Biswas PDF All Chapters
com
https://ebookmass.com/product/typescript-basics-learn-
typescript-from-scratch-and-solidify-your-skills-with-
projects-1st-edition-nabendu-biswas/
OR CLICK HERE
DOWLOAD NOW
https://ebookmass.com/product/practical-graphql-learning-full-stack-
graphql-development-with-projects-1st-edition-nabendu-biswas/
ebookmass.com
https://ebookmass.com/product/modern-typescript-a-practical-guide-to-
accelerate-your-development-velocity-1st-edition-ben-beattie-hood/
ebookmass.com
https://ebookmass.com/product/modern-typescript-1-converted-edition-
ben-beattie-hood/
ebookmass.com
https://ebookmass.com/product/bernard-bolzano-his-life-and-work-paul-
rusnock/
ebookmass.com
Korean from zero! All three books George Trombley
https://ebookmass.com/product/korean-from-zero-all-three-books-george-
trombley/
ebookmass.com
https://ebookmass.com/product/health-reforms-in-post-communist-
eastern-europe-the-politics-of-policy-learning-tamara-popic/
ebookmass.com
https://ebookmass.com/product/etextbook-978-0393265156-psychology-in-
your-life-2nd-edition-by-sarah-grison/
ebookmass.com
https://ebookmass.com/product/conditions-in-occupational-therapy-
effect-on-occupational-performance-5th-edition-ebook-pdf/
ebookmass.com
Everyday Creativity and the Healthy Mind: Dynamic New
Paths for Self and Society 1st ed. Edition Ruth Richards
https://ebookmass.com/product/everyday-creativity-and-the-healthy-
mind-dynamic-new-paths-for-self-and-society-1st-ed-edition-ruth-
richards/
ebookmass.com
TypeScript Basics
Learn TypeScript from Scratch
and Solidify Your Skills
with Projects
Nabendu Biswas
TypeScript Basics: Learn TypeScript from Scratch and Solidify Your Skills
with Projects
Nabendu Biswas
Bhopal, India
Introduction���������������������������������������������������������������������������������������xiii
v
Table of Contents
vi
Table of Contents
Decorators Setup������������������������������������������������������������������������������������������������69
Simple Decorators����������������������������������������������������������������������������������������������71
Decorator Factories���������������������������������������������������������������������������������������������73
Useful Decorators�����������������������������������������������������������������������������������������������74
Property Decorators��������������������������������������������������������������������������������������������75
Summary������������������������������������������������������������������������������������������������������������78
vii
Table of Contents
Index�������������������������������������������������������������������������������������������������161
viii
About the Author
Nabendu Biswas is a full stack JavaScript
developer. He has worked in the IT industry
for the past 18 years for the world’s top
development firms and investment banks.
He is a passionate tech blogger and YouTuber
and he currently works as an Architect in an
IT company at Bhopal. He is the author of five
books, all published by Apress. His books cover
Gatsby, MERN, and React Firebase.
ix
About the Technical Reviewer
Alexander Nnakwue has a background
in mechanical engineering. He is a senior
software engineer with over seven years of
experience in various industries, including
payments, blockchain, and marketing
technologies. He is a published author in
professional JavaScript, as well as a technical
writer and reviewer. Currently, he is working as
a software engineer at Konecranes, helping the
digital experience team with machine data and industrial cranes.
In his spare time, he loves to listen to music and enjoys the game of
soccer. He currently lives in Helsinki, Finland with his lovely wife.
xi
Introduction
TypeScript is revolutionizing how developers create JavaScript apps. It
was built by Microsoft to fix the issues that came out of loose binding in
JavaScript. Since JavaScript is a loosely typed language, a lot of issues
ended up in the production apps. These issues were hard to track and took
a lot of time to fix.
TypeScript is a superset of JavaScript, and it enables you to avoid type
errors before they even occur. You can catch them in an IDE (Integrated
Development Environment) like VS Code. The popular JavaScript frontend
framework of Angular uses TypeScript by default. The most popular
JavaScript frontend library called React also uses JavaScript by default.
This book first teaches you about TypeScript, and then you will use
it in a ReactJS project. You will also use it with the JavaScript backend
framework of NodeJS and learn how to create a React Redux project.
xiii
CHAPTER 1
Getting Started
Welcome to TypeScript Basics, where you’ll learn TypeScript from scratch
and solidify your skills by creating some projects. TypeScript is a superset
of JavaScript and was built by Microsoft to fix the issues of loose binding in
JavaScript.
After learning the fundamentals of TypeScript in the first six chapters,
you will use that information to create the following projects:
With TypeScript, you get all the new features of JavaScript, through
which you can avoid type errors before they occur. The limitation of
TypeScript is that browsers can’t execute it.
Browsers only understand JavaScript, so TypeScript needs to be
compiled to JavaScript. This chapter starts with the basic setup.
Project Setup
Open a new folder in VS Code and create a basic index.html file in it (see
Listing 1-1). This example also refers to a JavaScript file called main.js.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>TypeScript Basics</title>
</head>
<body>
<h1>TypeScript Basics</h1>
<script src="main.js"></script>
</body>
</html>
1 npm i -g typescript
Now, create the main.ts file and add a simple console.log to it. The
browser only understands JavaScript, so you have to change it to JavaScript
using the tsc main.ts command. The tsc command runs the TypeScript
compiler and converts the TypeScript file to JavaScript. This command will
create a new main.js JavaScript file in the same directory. See Figure 1-1.
2
Chapter 1 Getting Started
You will also be using the awesome extension of Live Server in this
project (see Figure 1-2) so that you don’t have to rerun the project after
every change.
3
Chapter 1 Getting Started
Summary
In this introductory chapter, you completed the basic setup for TypeScript.
You installed TypeScript globally and created basic HTML and TypeScript
files and made them work together.
4
CHAPTER 2
TypeScript Basics
This chapter starts your TypeScript journey. Here, you will learn about
different types and ways to use them in your projects. You will start with
the number type, followed by the string and Boolean types. You will also
learn about many more types and end by learning about interfaces.
//Numbers
let myNum = 10;
let anotherNum: number = 20;
myNum = 12;
myNum = '12';
anotherNum = 30;
anotherNum = false;
If you hover your mouse over the error, you can see the real issues, as
shown in Figures 2-1 and 2-2.
As you can see in this example, even if you don’t assign a type,
TypeScript infers a type. Hover the mouse over myNum and it will show the
number type. See Figure 2-3.
6
Chapter 2 TypeScript Basics
//String
let myStr: string = 'Hello';
let anotherStr = 'World';
myStr = true;
anotherStr = 45;
//Boolean
let myBool: boolean = true;
let anotherBool = false;
myBool = 'true';
anotherBool = 76;
Inference
So, you might wonder when to assign a type and when is it better to let
TypeScript automatically assign it?
7
Chapter 2 TypeScript Basics
//Inference
let salary;
salary = 12000;
salary = '12000';
salary = true;
Now, this is not right. When you want to assign a value later, you’ll
provide an explicit type.
You will now start getting type errors, as shown in Listing 2-4.
//Inference
let salary:number;
salary = 12000;
salary = '12000';
salary = true;
Objects
This section explains what objects are in TypeScript. In the example in
Listing 2-5, an object that has two strings is given one number and one
Boolean type.
If you hover your mouse over the object, it will indicate the data types.
8
Chapter 2 TypeScript Basics
//Objects
const developer = {
firstName: 'Nabendu',
lastName: 'Biswas',
age: 40,
isTrainer: true
}
Now create a new object where you will give the type of each key (see
Listing 2-6). You will get an error if you try to assign a different value to a
key or to a key that doesn’t exist. See Figure 2-4.
9
Chapter 2 TypeScript Basics
Arrays
This section looks at arrays. In TypeScript, if you provide an array of
strings, such as languages in the following example, you cannot push a
number or Boolean type to that array.
You can also explicitly declare that you have an array of a certain type,
such as declaring an array of number types. See Listing 2-7.
10
Chapter 2 TypeScript Basics
//Arrays
const languages = ['React', 'Angular', 'Vue'];
languages.push('TypeScript');
languages.push(56);
languages.push(true);
Complex Arrays
In this section, you learn to create an array of objects. You provide the type
and you indicate the type of keys in the object (see Listing 2-8).
If you want to write the array type, you need to use two brackets [] in
the type inference, as shown in Listing 2-9.
11
Chapter 2 TypeScript Basics
Functions
This section looks at the example of functions. Suppose you need to add
two numbers and create a function called addNums to do so.
If you don’t provide the type, you will not get an error even if you give
one string. See Listing 2-10.
//Functions
const addNums = (num1, num2) => {
return num1 + num2;
}
addNums(10, 20);
addNums(10, '20');
12
Chapter 2 TypeScript Basics
multiNums(10, 20);
multiNums(10, '20');
modNums(10, 20);
modNums(10, '20');
printSum(10, 20);
printSum(10, '20');
Union Types
You can also create union types, in which a variable can have multiple
types. Say you have a variable called numOrStr, which can be a number or
string type.
You can also have an array, which can only contain elements of the
number or string type. See Listing 2-12.
13
Chapter 2 TypeScript Basics
//Union types
let numOrStr: number | string;
numOrStr = 10;
numOrStr = 'Ten';
Literal Types
With literal types, you specify only the acceptable terms. For example, in
the myLiteral type, Nabendu, Mousam, Shikha, and Hriday are the only
acceptable values.
Listing 2-13 uses Parag and the program returns errors.
//Literal types
let myLiteral: 'Nabendu' | 'Mousam' | 'Shikha' | 'Hriday' =
'Nabendu';
myLiteral = 'Mousam';
myLiteral = 'Shikha';
myLiteral = 'Hriday';
myLiteral = 'Parag';
Enum Types
This section looks at enum, which is a combination of the union type and
the literal type. Listing 2-14 provides a predefined type with the enum
variable.
14
Chapter 2 TypeScript Basics
//Enum
enum Role { ADMIN, READ_ONLY, AUTHOR };
Optionals Type
This section looks at optionals. Suppose you want an age field, which
should be a number.
In the example in Listing 2-15, in optionalObj, the age is declared as
a number and is undefined. The problem with this approach is that you
need to leave it undefined if you don’t want to specify it.
In the betterOptObj example, the age is indicated with ?, which means
if you provide it, it should be a number, but it is not required.
//Optionals
let optionalObj: { name: string; age: number | undefined } = {
name: 'Nabendu',
age: undefined
};
15
Chapter 2 TypeScript Basics
//Interfaces
interface Developer {
name: string;
age: number;
isDev: boolean;
}
Types are similar to interfaces. As you can see in Listing 2-17, they are
used in DeveloperType.
Interfaces can be used only in objects, whereas types can be used in
strings, arrays of objects, or anything else.
16
Chapter 2 TypeScript Basics
//Types
type DeveloperType = {
name: string;
age: number;
isDev: boolean;
}
type CoderType = {
name: string;
category: 'frontend' | 'backend' | 'mobile';
age: number;
}[];
17
Chapter 2 TypeScript Basics
//Types
type DeveloperNewType = {
name: string;
age: number;
isDev: boolean;
}
const person5: DeveloperNewType = {
name: 'Nabendu',
age: 40,
isDev: true
}
console.log('${person5.name} is a ${person5.isDev} Dev and is
${person5.age} years old');
Now, in the localhost, you can see the desired console log (see
Figure 2-5).
18
Chapter 2 TypeScript Basics
Summary
In this chapter, you learned about different types in TypeScript. You
learned about the number type and then moved to string and Boolean
types. After learning about object and array types, you learned about
complex arrays. Then you learned about union, literal, enum, and optional
types. The chapter ended by discussing interfaces.
19
CHAPTER 3
The TypeScript
Compiler
When you run the TypeScript file every time, you are making changes to it.
In this chapter, you learn about other ways to watch for and make changes.
Watch Mode
You can watch the changes in the main.js file by using watch mode. Then
you don’t have to run the file after each change. You need to run the tsc
command with the -w flag, as follows:
1 tsc main.ts -w
If you then add anything to the file, it will be converted into the
corresponding JavaScript file. See Listing 3-1.
You can see the new changes in the localhost (see Figure 3-1).
22
Chapter 3 The TypeScript Compiler
You can then run the tsc command to compile all the TypeScript files.
However, the index.ts file has a lot of errors. Figure 3-3 shows one of
these errors.
23
Chapter 3 The TypeScript Compiler
24
Chapter 3 The TypeScript Compiler
You can also exclude all the node_modules, which are created when
you use a third-party library. Sometimes they contain TypeScript code and
you don’t want to compile them.
Similar to using exclude, you have use include to add the mentioned
files. In the example in Figure 3-5, the main.ts file is included.
25
Chapter 3 The TypeScript Compiler
26
Chapter 3 The TypeScript Compiler
You can now see the main.js file in the dist folder (see Figure 3-7).
You also need to change the path of the main.js file in the index.html file.
Summary
In this chapter, you learned to configure your TypeScript projects properly.
27
CHAPTER 4
Classes and
Interfaces
This chapter covers classes and interfaces. In order to follow along, you
should have basic knowledge of ES6 classes.
//Classes
class CreateRoom{
public room: string;
private family: string[] = [];
constructor(name: string){
this.room = '${name}s room'
}
addFamilyMember(member: string){
this.family.push(member);
}
showFamily(){
console.log(this.family);
}
cleanRoom(soap: string){
console.log('Cleaning ${this.room} with ${soap}');
}
}
//Classes
class CreateRoom{
public room: string;
private family: string[] = [];
readonly dobShikha: string = '1982-12-12';
30
Chapter 4 Classes and Interfaces
class CreateRoom{
private family: string[] = [];
readonly dobShikha: string = '1982-12-12';
private readonly dobHriday: string = '2013-12-12';
constructor(public room: string){
}
...
...
cleanRoom(soap: string){
console.log('Cleaning ${this.room} with ${soap}');
}
}
31
Random documents with unrelated
content Scribd suggests to you:
at Celorico, 124, 126, 134;
Francozo, 136;
his disposition of the troops, 174;
blockades Ciudad Rodrigo, 179, 196, 218;
invests Almeida, 179;
his victory of Fuentes d’Onoro, 179;
at St Vincente, 186;
title of Count of Vimiero, 201;
attack on Badajos, 218, 231, 234;
enters Castile, 220;
at Alfaiates, 252;
his plan of enlisting the Spaniards, 256;
his secret plans, 259, 272, 275;
moves on Rueda, 274;
at Villa Verde, 274;
formation of the army at the battle of Salamanca, 286;
pursuit of the enemy, 289, 292
Westmorland, 180, 184
Weyland, General, taken prisoner at Badajos, 243
White, Captain, his attack on the French at Ladoeiro, 164
White, Mr, wounded at the battle of Salamanca, 292
Whitelock, General, in command of an expedition in America, 1
Whittingham, appointed Colonel in the Spanish service, 23;
Brigadier, 75;
wounded, 75
Wilson, Major, wounded at the battle of Vimiero, 39
Wilson, Sir R., his defence of the Pass of Baños, 75;
his characteristics, 122;
his advance on Coimbra, 173
Zafra, 232
Zamora, 61, 76, 77;
evacuated, 297
Zamora bridge, 271
Zapardiel river, 274
Zeller, Frank van, 108
Zezere, 176
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must,
at no additional cost, fee or expense to the user, provide a copy,
a means of exporting a copy, or a means of obtaining a copy
upon request, of the work in its original “Plain Vanilla ASCII” or
other form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookmass.com