TypeScript







Introuduction

TypeScript is a free and open-source programming language developed and maintained by Microsoft. It is a strict superset of JavaScript, and adds optional static typing and class-based object-oriented programming to the language.

TypeScript compiles to clean, simple JavaScript code which runs on any browser, in Node.js, or in any JavaScript engine that supports ECMAScript 3 (or newer).

Installation
The command-line TypeScript compiler can be installed as a Node.js package.
npm install typescript -g
tsc -v
Version 2.2.2

Data Types
var flag:boolean = true;
var num:number = 100;
var myString:string = "TypeScript";
var myArr:number[] = [1,2,3];
var myArr1:any[] = [1,2,3,"abc"];
var num1:any = 110;
num1 = "abc";
const PI = 3.14;
enum myConstants {
 pi = 3.14,
 e = 2.73,
 log2 = 0.3
}
var radius:number = 7;
var cir = 2*myConstants.pi*radius;
console.log(cir);
JS
var flag = true;
var num = 100;
var myString = "TypeScript";
var myArr = [1, 2, 3];
var myArr1 = [1, 2, 3, "abc"];
var num1 = 110;
num1 = "abc";
var PI =3.14
var myConstants;
(function (myConstants) {
    myConstants[myConstants["pi"] = 3.14] = "pi";
    myConstants[myConstants["e"] = 2.73] = "e";
    myConstants[myConstants["log2"] = 0.3] = "log2";
})(myConstants || (myConstants = {}));
var radius = 7;
var cir = 2 * myConstants.pi * radius;
console.log(cir);
Functions
function addNums(num1:number, num2:number):number{
 return num1+num2;
}
console.log(addNums(30,28));

function addNums1(num1:any, num2:number):number{
 if(typeof num1 == "string"){
  if(isNaN(parseInt(num1,10))){
   return 0;
  }
  num1 = parseInt(num1,10);
 }
 return num1+num2;
}
console.log(addNums1('30',28));

//function with default argument
function area(r1:number,r2:number = r1){
 return Math.PI*r1*r2;
}
console.log(area(7));

//function with optional argument
function areaofsquare(side:number,isInteger?:boolean):number{
 if(isInteger){
  return Math.floor(side*side);
 }
 return side*side;
}
console.log(areaofsquare(7.25));
console.log(areaofsquare(7.25,true));


//function overloading
function areaofsquad(side1:number);
function areaofsquad(side1:number, side2:number);
function areaofsquad(side1:number,side2:number,side3:number,side4:number);
function areaofsquad(side1:number,side2?:number,side3?:number,side4?:number){
 if(side2 === undefined && side3 === undefined && side4 === undefined){
  side3=side2=side1;
  return side1*side2;
 }
 else if(side3 === undefined && side4===undefined){
  side3 = side1;
  side4 = side2;
  return side1+side2;
 }
 return -1;
}
areaofsquad(1);
areaofsquad(1,2);
areaofsquad(1,2,3,4);

JS
function addNums(num1, num2) {
    return num1 + num2;
}
console.log(addNums(30, 28));
function addNums1(num1, num2) {
    if (typeof num1 == "string") {
        if (isNaN(parseInt(num1, 10))) {
            return 0;
        }
        num1 = parseInt(num1, 10);
    }
    return num1 + num2;
}
console.log(addNums1('30', 28));
//function with default argument
function area(r1, r2) {
    if (r2 === void 0) { r2 = r1; }
    return Math.PI * r1 * r2;
}
console.log(area(7));
//function with optional argument
function areaofsquare(side, isInteger) {
    if (isInteger) {
        return Math.floor(side * side);
    }
    return side * side;
}
console.log(areaofsquare(7.25));
console.log(areaofsquare(7.25, true));
function areaofsquad(side1, side2, side3, side4) {
    if (side2 === undefined && side3 === undefined && side4 === undefined) {
        side3 = side2 = side1;
        return side1 * side2;
    }
    else if (side3 === undefined && side4 === undefined) {
        side3 = side1;
        side4 = side2;
        return side1 + side2;
    }
    return -1;
}
areaofsquad(1);
areaofsquad(1, 2);
areaofsquad(1, 2, 3, 4);
function operate(x) {
    return x.side * x.side;
}
Interface
interface operateInterface{
 shape:string;
 side?:number;
}

function operate(x:operateInterface){
 return x.side*x.side;
}
var calc = operate({shape:"square",side:4});

interface player{
 run():void;
 addLives(n:number):void;
 score():number;
}

function createPlayer() : player{
 return {
  run:function(){},
  addLives:function(n:number){},
  score: function(){ return -1}
 }
}

var player1 = createPlayer();
JS
var calc = operate({ shape: "square", side: 4 });
function createPlayer() {
    return {
        run: function () { },
        addLives: function (n) { },
        score: function () { return -1; }
    };
}
var player1 = createPlayer();
Class
class website{
 url:string;
 fbLikes:number;
 constructor(url:string, fbLikes:number){
  this.url = url;
  this.fbLikes = fbLikes;
 }

 likesInK():string{
  return this.fbLikes/1000+'k';
 }
}

var google = new website("http://www.google.com",1344);

console.log(google.likesInK());

class Rectangle{
 l1:number;
 l2:number;
 constructor(l1:number,l2:number){
  this.l1 = l1;
  this.l2 = l2;
 }
 get area(){
  return this.l1*this.l2;
 }

 set side1(length1:number){
  this.l1 = length1;
 }
 set side2(length2:number){
  this.l2 = length2;
 }

 printAllInfo(){
  console.log("L1"+this.l1,"L2"+this.l2)
 }
}
var rect = new Rectangle(7,6);
console.log(rect.area);

rect.side1 = 10;
rect.side2 = 20;
rect.printAllInfo();
console.log(rect.area);
JS
var website = (function () {
    function website(url, fbLikes) {
        this.url = url;
        this.fbLikes = fbLikes;
    }
    website.prototype.likesInK = function () {
        return this.fbLikes / 1000 + 'k';
    };
    return website;
}());
var google = new website("http://www.google.com", 1344);
console.log(google.likesInK());
var Rectangle = (function () {
    function Rectangle(l1, l2) {
        this.l1 = l1;
        this.l2 = l2;
    }
    Object.defineProperty(Rectangle.prototype, "area", {
        get: function () {
            return this.l1 * this.l2;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Rectangle.prototype, "side1", {
        set: function (length1) {
            this.l1 = length1;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Rectangle.prototype, "side2", {
        set: function (length2) {
            this.l2 = length2;
        },
        enumerable: true,
        configurable: true
    });
    Rectangle.prototype.printAllInfo = function () {
        console.log("L1" + this.l1, "L2" + this.l2);
    };
    return Rectangle;
}());
var rect = new Rectangle(7, 6);
console.log(rect.area);
rect.side1 = 10;
rect.side2 = 20;
rect.printAllInfo();
console.log(rect.area);
For Each
var randArray = [5,6,7,8];
for(var val in randArray){
 document.write(val + "
"); } for(var val in randArray){ document.write(randArray[val] + "
"); } var strArray = randArray.map(String); for(var val of strArray){ document.write(val + "
"); }
JS
var randArray = [5, 6, 7, 8];
for (var val in randArray) {
    document.write(val + "
"); } for (var val in randArray) { document.write(randArray[val] + "
"); } var strArray = randArray.map(String); for (var _i = 0, strArray_1 = strArray; _i < strArray_1.length; _i++) { var val = strArray_1[_i]; document.write(val + "
"); }
class Animal{
 public favFood: string;
 static numOfAnimals:number = 0; 
 constructor(private name:string, private owner:string){
  Animal.numOfAnimals++;
 }
 ownerInfo(){
  document.write(this.name + " is owned by "+this.owner+"
"); } static howManyAnimals():number{ return Animal.numOfAnimals; } private _weight:number; get weight():number{ return this._weight; } set weight(weight:number){ this._weight = weight; } } var spot = new Animal("spot","Dough"); spot.ownerInfo(); spot.weight = 100; document.write("Spot's weight is "+ spot.weight + "
"); document.write("Num of Animals "+Animal.howManyAnimals() + "
"); class Dog extends Animal{ constructor(name:string,owner:string){ super(name,owner); Dog.numOfAnimals++; } } var grover = new Dog("Grover","Jimmy"); document.write("No of Animals "+ Animal.howManyAnimals()+"
"); document.write("Is grover Animal? "+ (grover instanceof Animal)+"
"); document.write("Does grover have a name? "+ ('name' in grover)+"
"); interface Vechicle{ drive() : any; } class Car implements Vechicle { constructor(private wheel: number) { // code... } drive() : void{ document.write("The car drives with " + this.wheel+"
") } } class Bicycle implements Vechicle { constructor(private wheel: number) { // code... } drive() : void{ document.write("The bicycle drives with " + this.wheel+"
") } } var c = new Car(4); var b = new Bicycle(2); c.drive(); b.drive(); function GetType(val: T): string{ return typeof(val); } var aStr = "A String"; var aNum = 10; document.write(GetType(aStr)+"
"); document.write(GetType(aNum)+"
"); function GetWheels(veh:W):number{ return veh.drive(); } GetWheels(c); GetWheels(b); class GenericNumber{ add: (val1: T, val2: T) => T; } var aNumber = new GenericNumber(); aNumber.add = function(x,y){ return x+y; } document.write("4 +8 = " + aNumber.add(4,8) +"
"); var aStrNum = new GenericNumber(); aStrNum.add = function(x,y){ return String(Number(x)+Number(y)); } document.write("4 +1 = " + aStrNum.add("4","1") +"
");
JS
var Animal = (function () {
    function Animal(name, owner) {
        this.name = name;
        this.owner = owner;
        Animal.numOfAnimals++;
    }
    Animal.prototype.ownerInfo = function () {
        document.write(this.name + " is owned by " + this.owner + "
"); }; Animal.howManyAnimals = function () { return Animal.numOfAnimals; }; Object.defineProperty(Animal.prototype, "weight", { get: function () { return this._weight; }, set: function (weight) { this._weight = weight; }, enumerable: true, configurable: true }); return Animal; }()); Animal.numOfAnimals = 0; var spot = new Animal("spot", "Dough"); spot.ownerInfo(); spot.weight = 100; document.write("Spot's weight is " + spot.weight + "
"); document.write("Num of Animals " + Animal.howManyAnimals() + "
"); var Dog = (function (_super) { __extends(Dog, _super); function Dog(name, owner) { var _this = _super.call(this, name, owner) || this; Dog.numOfAnimals++; return _this; } return Dog; }(Animal)); var grover = new Dog("Grover", "Jimmy"); document.write("No of Animals " + Animal.howManyAnimals() + "
"); document.write("Is grover Animal? " + (grover instanceof Animal) + "
"); document.write("Does grover have a name? " + ('name' in grover) + "
"); var Car = (function () { function Car(wheel) { this.wheel = wheel; // code... } Car.prototype.drive = function () { document.write("The car drives with " + this.wheel + "
"); }; return Car; }()); var Bicycle = (function () { function Bicycle(wheel) { this.wheel = wheel; // code... } Bicycle.prototype.drive = function () { document.write("The bicycle drives with " + this.wheel + "
"); }; return Bicycle; }()); var c = new Car(4); var b = new Bicycle(2); c.drive(); b.drive(); function GetType(val) { return typeof (val); } var aStr = "A String"; var aNum = 10; document.write(GetType(aStr) + "
"); document.write(GetType(aNum) + "
"); function GetWheels(veh) { return veh.drive(); } GetWheels(c); GetWheels(b); var GenericNumber = (function () { function GenericNumber() { } return GenericNumber; }()); var aNumber = new GenericNumber(); aNumber.add = function (x, y) { return x + y; }; document.write("4 +8 = " + aNumber.add(4, 8) + "
"); var aStrNum = new GenericNumber(); aStrNum.add = function (x, y) { return String(Number(x) + Number(y)); }; document.write("4 +1 = " + aStrNum.add("4", "1") + "
");

0 comments:

Post a Comment

Search This Blog

Powered by Blogger.