Wednesday, 17 February 2016

What is TypeScript

What is TypeScript

Type script is superset of javascript it compile on top of javascript only.  When Type script comple it will convert to javascript only. We are saying it as type script as it provide the Type safe javascript format. It uses the concept of OOPS and implement the javascript as OOPS based.
e.g. in Javascript we defined variable like;
var v = 20; // We don’t defined the type.
But if you see in type script it will be
var v : number = 20; // We defined the type
Command to install Type Script in node.js
npm install -g typescript

You can download the type script from Microsoft by given below link:

Basic types in Typescript

Basic Data type for Type script

var IsAvilable: boolean = false;
var Height: number = 6;

var Name: string = "satya";

var Student: number[] = [1, 2, 3];

// Enum
enum Color { Red = 5, Green, Blue }

//Defining any type that you don’t know
var value: any = "test";
value = 1;

//You can use void as the return type like

Defining the Method in Type Script:

The way of defining method in type script.

/// Defining method where return type in void
function IsExist() : void {
    console.log("random stuff");

/// Defining method where return type in String
function IsExist(card :  string): string {
    console.log("random stuff");
    return card;

Defining A class in Type script

class Myclass {

    private greeting: string; // private varaiable
myMessage: string;
    constructor(greetingmessage: string) {
        this.greeting = greetingmessage;

    greet() {
        console.log("Your Message {0}", this.greeting);

Access class;
var g = new Myclass("message");

Passing Optional Parameter in constructor
constructor(greetingmessage: string, Message? : string) {
        this.greeting = greetingmessage;
        this.myMessage = Message;

Defining the Interface in Type Script:

interface iCreater {
    (x: string): string; /// Defining method
    message: string/// Defining Property

Inheritance in TypeScript

class Vehicle {
    constructor(name: string){ }
    wheel(noofWheel: number) {
        console.log("Number of Wheel :" + noofWheel);

class Car extends Vehicle {
    constructor(name: string) {
        //calling the base constructor
    wheel(noofWheel: number) {
        console.log("Number of Car Wheel :" + noofWheel);


var v = new Car("car");

Defining the namespace for your classes

You need to define the module
module Game.CardSpace {
    class Vehicle {
        constructor(name: string) { }
        wheel(noofWheel: number) {
            console.log("Number of Wheel :" + noofWheel);

Getting references of 1.ts file to another 2.ts file

Need to take the reference of 1.ts like
/// <reference path=1.ts' />
And classes defined in the 1.ts or value need to give as export e.g.
module Game.CardSpace {

    // Export keyword make your variable as public
   export enum CardType  { Spades, Hearts, clubs, Diamond };

Other feature and implementation of OOPS:

No comments:

Post a Comment