Wednesday, January 25, 2017

2-Typescript - Language used in Angular 2JS

TypeScript: Angular2JS is written in Typescript. Its a superset of javascript. It has a few additional rules as below:

1)myVar = "astring"; 
myVar = 23; (now assigned to an integer) allowed in javascript.
However, this isnt allowed in Typescript to change the type of the variable.

2)Unlike in javascript, in Typescript, we could declare the type o the variable as below. Its optional
Ex: Types are :
  • string.Ex: myVar:string="hello" (or) myVar="hello"
  • numberEx: myVar:number=21 (or) myVar=21
  • booleanEx: myVar:boolean=true (or) myVar=true
  • arraysEx: myVar:number[]
  • any type as inEx: myVar: any ; myVar=50.
  • classesEx: myVar:<<class-name>>=new <<class-name>>()// as in java
    • Ex: Class definition: 
                        class Car            
                        {
            
                         speed:number; 
                                    constructor(mph:number)
                                    {
                                                this.speed = mph;
                                    }
                        }
  • Class instance: 
                        myCar:Car = new Car(70);// instance creation using number constructor

--------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment