Installation
- For Visual Studio 2012, use the link to download. Download and Install the Typescript plugin for Visual Studio
- For Visual Studio 2013, Download Visual Studio Update 2
Configuration
1- Method parameters: following example show how to define the typed parameters to the function. in the following line, it shows that meters is of type number.
move(meters: number) {}
2- Variable type: Following example informs how to define variables of type string.
greeting: string;
3- Defining Class: Following code shows how to define the class,
class Animal {
}
4- Constructor: Following code shows how to define constructor
class Animal {
constructor(public name: string) { }
}
5- Inheritance: Following code shows how to define inheritance,
class Horse extends Animal {
}
6- Calling Base Class Method: Following code shows how to call base class method,
super.move(5);
Complete Code
class Animal {
constructor(public name: string) { }
move(meters: number) {
alert(this.name + " moved " + meters + "m.");
}
}
class Snake extends Animal {
constructor(name: string) { super(name); }
move() {
alert("Slithering...");
super.move(5);
}
}
class Horse extends Animal {
constructor(name: string) { super(name); }
move() {
alert("Galloping...");
super.move(45);
}
}
var sam = new Snake("Sammy the Python");
var tom: Animal = new Horse("Tommy the Palomino");
sam.move();
tom.move(34);
No comments:
Post a Comment