Ch. 2 TYPESCRIPT
Ch. 2 TYPESCRIPT
CHAPTER - 2
Overview of Typescript
. Cross-Platform : TypeScript runs on any platform that JavaScript runs on. The TypeScript
1
compiler can be installed on any Operating System such as Windows, macOS, and Linux.
2. Object-Oriented Language : TypeScript provides powerful features such as Classes, Interfaces,
and Modules. You can write pure object-oriented code for client-side as well as server-side
development.
3. Static type-checking : TypeScript uses static typing. This is done using type annotations. It
helps type checking at compile time. Thus, you can find errors while typing the code without
running your script each time. Additionally, using the type inference mechanism, if a variable is
declared without a type, it will be inferred based on its value.
Features of Typescript
6. The TypeScript Definition file, with .d.ts extension, provides support for existing JavaScript libraries
like JQuery, D3.js, etc. So, TypeScript code can add JavaScript libraries using type definitions to avail the
benefits of type-checking, code auto-completion, and documentation
In existing dynamically-typed JavaScript libraries.
7. TypeScript has support for the latest JavaScript features from ECMAScript 2015. It includes features
from ES6 and ES7 that can run in ES5-level JavaScript engines like Node.js. This offers a massive
advantage of using features from future JavaScript versions in current JavaScript engines.
8. TypeScript has easy integration with task runner tools like Grunt and Gulp to automate the workflow.
TypeScript Architecture
Language : It features the TypeScript language ,elements. It consists of syntax, keywords,
and type annotations.
The TypeScript Compiler : The TypeScript compiler (tsc) changes the instructions written
in TypeScript to its JavaScript equivalent.
Example
$ tschelloworld.ts //
It compiles the TS file helloworld into the helloworld.js file
The TypeScript Language Service − The “Language Service” reveals an extra layer around
the core language service assists the common set of typical editor operations like statement
completion, signature help, code formatting and outlining, colorization, etc.
Types of Typescript
TypeScript Data Types Syntax
console.log("Name:" + uname);
x An Operator is a symbol which operates on a value or data.
x It represents a specific action on working with data.
x The data on which operators operates is called operand.
x It can be used with one or more than one values to produce a single value.
x All of the standard JavaScript operators are available with the TypeScript program.
x Example
10 + 20 = 30;
In the above example, the values ‘10’, ‘20’ and ‘30’ are known as an operand, whereas
'+' and '=' are known as
operators.
DECISION MAKING IN TYPESCRIPT
Var num:number = 2
if(num> 0) {
console.log(num+" is positive")
} else if(num< 0) {
console.log(num+" is negative")
} else {
console.log(num+" is neither positive nor negative")
}
LOOPS IN TYPESCRIPT
x A loop whose number of iterations are definite/fixed is
termed as a definite loop. The for loop is an implementation of a definite loop.
x The for loop executes the code block for a specified
number of times. It can be used to iterate over a fixed
set of values, such as an array.
var num:number = 5;
var i:number;
var factorial = 1;
for(i = num;i>=1;i--) {
factorial *= i;
console.log(factorial);
x An indefinite loop is used when the number of
iterations in a loop is indeterminate or unknown.
x Indefinite loops can be implemented using while and do...while loop.
var num:number = 5;
var factorial:number = 1;
while(num>=1) {
factorial = factorial * num;
num--;
}
console.log("The factorial is "+factorial);
TYPESCRIPT FUNCTIONS
1. Code reusability : We can call a function several times without writing the same block of code
again. The code reusability saves time and reduces the program size.
2. Less coding : Functions makes our program compact. So, we don't need to write many lines of code
each time to perform a common task.
3. Easy to debug : It makes the programmer easy to locate and isolate faulty information.
//Function Definition
function display() {
console.log("Hello WebX.0!");
//Function Call
display();
TypeScript Arrow Function (Lambda Function)
x In the above example, sum is an arrow function. (x:number, y:number) denotes the
parameter types, :number specifies the return type. The fat arrow =>
separates the function parameters and the function body. The right side of => can contain
one or more code statements.
TypeScript Function Overloading
class Student {
Sid:number;
Sname:string;
display():void {
}
Data Hiding
x A class can control the visibility of its data members from the members of the other classes.
This capability is termed as encapsulation or data-hiding.
x The access modifier defines the visibility of class data member outside its defining class.
x TypeScript supports the three types of access modifier. These are: Public, Private and
Protected.
class Student {
public studCode: number;
studName: string;
}
let stud = new Student();
stud.studCode = 101;
stud.studName = "Sachin Shah";
console.log(stud.studCode+ " "+stud.studName);