Typescript Tutorial
Typescript Tutorial
TypeScript is pure object oriented with classes, interfaces and statically typed like C# or
Java. The popular JavaScript framework Angular 2.0 is written in TypeScript. Mastering
TypeScript can help programmers to write object-oriented programs and have them
compiled to JavaScript, both on server side and client side.
Audience
Programmers coming from Object Oriented world will find it easy to use TypeScript. With
the knowledge of TypeScript, they can build web applications much faster, as TypeScript
has good tooling support.
Prerequisites
As a reader of this tutorial, you should have a good understanding of OOP concepts and
basic JavaScript, to make the most of this tutorial.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at [email protected]
i
TypeScript
Table of Contents
About the Tutorial .................................................................................................................................... i
Audience .................................................................................................................................................. i
Prerequisites ............................................................................................................................................ i
1. TYPESCRIPT – OVERVIEW..................................................................................................... 1
What is TypeScript?................................................................................................................................. 1
Features of TypeScript............................................................................................................................. 1
Installing Node.js..................................................................................................................................... 5
Brackets ................................................................................................................................................ 11
ii
TypeScript
6. TYPESCRIPT ─ OPERATORS................................................................................................. 28
Assignment Operators........................................................................................................................... 36
The if Statement.................................................................................................................................... 42
iii
TypeScript
Syntactic Variations............................................................................................................................... 77
toExponential() ..................................................................................................................................... 85
toFixed() ................................................................................................................................................ 86
iv
TypeScript
toLocaleString() ..................................................................................................................................... 87
toPrecision() .......................................................................................................................................... 87
toString() ............................................................................................................................................... 88
valueOf() ............................................................................................................................................... 89
charAt ................................................................................................................................................... 93
charCodeAt() ......................................................................................................................................... 94
concat() ................................................................................................................................................. 95
indexOf() ............................................................................................................................................... 95
lastIndexOf() ......................................................................................................................................... 96
localeCompare() .................................................................................................................................... 97
replace() ................................................................................................................................................ 98
search() ................................................................................................................................................. 99
substr()................................................................................................................................................ 101
v
TypeScript
every()................................................................................................................................................. 112
reduce()............................................................................................................................................... 120
vi
TypeScript
vii
TypeScript
viii
1. TypeScript – Overview TypeScript
JavaScript was introduced as a language for the client side. The development of Node.js
has marked JavaScript as an emerging server-side technology too. However, as JavaScript
code grows, it tends to get messier, making it difficult to maintain and reuse the code.
Moreover, its failure to embrace the features of Object Orientation, strong type checking
and compile-time error checks prevents JavaScript from succeeding at the enterprise level
as a full-fledged server-side technology. TypeScript was presented to bridge this gap.
What is TypeScript?
By definition, “TypeScript is JavaScript for application-scale development.”
Features of TypeScript
TypeScript is just JavaScript. TypeScript starts with JavaScript and ends with
JavaScript. Typescript adopts the basic building blocks of your program from JavaScript.
Hence, you only need to know JavaScript to use TypeScript. All TypeScript code is
converted into its JavaScript equivalent for the purpose of execution.
JavaScript is TypeScript. This means that any valid .js file can be renamed to .ts and
compiled with other TypeScript files.
1
TypeScript
Additional
ECMASCRIPT5 ECMASCRIPT6 TypeScript
Features
TypeScript adopts its basic language features from the ECMAScript5 specification, i.e., the
official specification for JavaScript. TypeScript language features like Modules and class-
based orientation are in line with the EcmaScript 6 specification. Additionally, TypeScript
also embraces features like generics and type annotations that aren’t a part of the
EcmaScript6 specification.
Strong Static Typing: JavaScript is not strongly typed. TypeScript comes with an
optional static typing and type inference system through the TLS (TypeScript
Language Service). The type of a variable, declared with no type, may be inferred
by the TLS based on its value.
2
TypeScript
Components of TypeScript
At its heart, TypeScript has the following three components:
The TypeScript Compiler: The TypeScript compiler (tsc) converts the instructions
written in TypeScript to its JavaScript equivalent.
Declaration Files
When a TypeScript script gets compiled, there is an option to generate a declaration file
(with the extension .d.ts) that functions as an interface to the components in the compiled
JavaScript. The concept of declaration files is analogous to the concept of header files
found in C/C++. The declaration files (files with .d.ts extension) provide intellisense for
types, function calls, and variable support for JavaScript libraries like jQuery, MooTools,
etc.
3
2. TypeScript – Environment Setup TypeScript
In this chapter, we will discuss how to install TypeScript on Windows platform. We will also
explain how to install the Brackets IDE.
var num:number=12
console.log(num)
12
4
TypeScript
A Text Editor
The text editor helps you to write your source code. Examples of a few editors include
Windows Notepad, Notepad++, Emacs, vim or vi, etc. Editors used may vary with
Operating Systems.
The source files are typically named with the extension .ts
The TSC generates a JavaScript version of the .ts file passed to it. In other words, the TSC
produces an equivalent JavaScript source code from the Typescript file given as an input
to it. This process is termed as transpilation.
However, the compiler rejects any raw JavaScript file passed to it. The compiler deals with
only .ts or .d.ts files.
Installing Node.js
Node.js is an open source, cross-platform runtime environment for server-side JavaScript.
Node.js is required to run JavaScript without a browser support. It uses Google V8
JavaScript engine to execute code. You may download Node.js source code or a pre-built
installer for your platform. Node is available here: https://nodejs.org/en/download
Installation on Windows
Follow the steps given below to install Node.js in Windows environment.
5
TypeScript
Step 2: To verify if the installation was successful, enter the command node –v in the
terminal window.
Step 3: Type the following command in the terminal window to install TypeScript.
Installation on Mac OS X
To install node.js on Mac OS X, you can download a pre-compiled binary package which
makes a nice and easy installation. Head over to http://nodejs.org/ and click the install
button to download the latest package.
6
TypeScript
Install the package from the .dmg by following the install wizard which will install both
node and npm. npm is Node Package Manager which facilitates installation of additional
packages for node.js.
7
TypeScript
Installation on Linux
You need to install a number of dependencies before you can install Node.js and NPM.
Ruby and GCC. You’ll need Ruby 1.8.6 or newer and GCC 4.2 or newer.
Homebrew. Homebrew is a package manager originally designed for Mac, but it’s
been ported to Linux as Linuxbrew. You can learn more about Homebrew at
http://brew.sh and Linuxbrew at http://brew.sh/linuxbrew.
Once these dependencies are installed, you may install Node.js by using the following
command on the terminal:
IDE Support
Typescript can be built on a plethora of development environments like Visual Studio,
Sublime Text 2, WebStorm/PHPStorm, Eclipse, Brackets, etc. Visual Studio Code and
Brackets IDEs are discussed here. The development environment used here is Visual
Studio Code (Windows platform).
Installation on Windows
Step 1: Download Visual Studio Code for Windows.
8
TypeScript
9
TypeScript
Step 4: You may directly traverse to the file’s path by right clicking on the file open in
command prompt. Similarly, the Reveal in Explorer option shows the file in the File
Explorer.
10
TypeScript
Installation on Mac OS X
Visual Studio Code’s Mac OS X specific installation guide can be found at
https://code.visualstudio.com/Docs/editor/setup
Installation on Linux
Linux specific installation guide for Visual Studio Code can be found at
https://code.visualstudio.com/Docs/editor/setup
Brackets
Brackets is a free open-source editor for web development, created by Adobe Systems. It
is available for Linux, Windows and Mac OS X. Brackets is available at http://brackets.io
11
TypeScript
Post installation, click on the extension manager icon on the right-hand side
of the editor. Enter typescript in the search box.
You can run DOS prompt / shell within Brackets itself by adding one more extension
Brackets Shell.
12
TypeScript
Upon installation, you will find an icon of shell on the right-hand side of the editor .
Once you click on the icon, you will see the shell window as shown below:
Note: Typescript is also available as a plugin for Visual Studio 2012 and 2013
environments (http://www.typescriptlang.org/#Download).VS 2015 and above includes
Typescript plugin by default.
13
3. TypeScript ─ Basic Syntax TypeScript
Syntax defines a set of rules for writing programs. Every language specification defines its
own syntax. A TypeScript program is composed of:
Modules
Functions
Variables
Statements and Expressions
Comments
Line 2 prints the variable’s value to the prompt. Here, console refers to the terminal
window. The function log () is used to display text on the screen.
Step 1: Save the file with .ts extension. We shall save the file as Test.ts. The code editor
marks errors in the code, if any, while you save it.
Step 2: Right-click the TypeScript file under the Working Files option in VS Code’s Explore
Pane. Select Open in Command Prompt option.
14
TypeScript
Step 3: To compile the file use the following command on the terminal window:
tsc Test.ts
Step 4: The file is compiled to Test.js. To run the program written, type the following in
the terminal:
node Test.js
15
TypeScript
16