TypeScript is just a tool to be used at coding-time to improve the quality of your JavaScript and make it more readable and maintainable. After your TypeScript is compiled to JavaScript, you can use all your other tools—minifiers, packagers, runtime loaders, unit test frameworks, and so forth—as you would if you had written the JavaScript from scratch.
TypeScript is an open-source language created by Microsoft, announced in October 2012. TypeScript is in the same line as CoffeScript or Dart for example. Optional static typing and class-based POO, TypeScript is designed for development of large applications and transcript it to JavaScript. The different features look like what you have in C# with Primitive type (number, bool, string, ...), generics or the object syntax. The language has the principal features of object with Modules, classes, interface, … but doesn’t have protected
and abstract
keywords.
At the present time, we are at version 0.9.5 and in version 1.x, the roadmap includes Async
/await
, Mixins (abstract
? extension method?) and protected
access.
TypeScript 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);
Same Code in JavaScript
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var Animal = (function () {
function Animal(name) {
this.name = name;
}
Animal.prototype.move = function (meters) {
alert(this.name + " moved " + meters + "m.");
};
return Animal;
})();
var Snake = (function (_super) {
__extends(Snake, _super);
function Snake(name) {
_super.call(this, name);
}
Snake.prototype.move = function () {
alert("Slithering...");
_super.prototype.move.call(this, 5);
};
return Snake;
})(Animal);
var Horse = (function (_super) {
__extends(Horse, _super);
function Horse(name) {
_super.call(this, name);
}
Horse.prototype.move = function () {
alert("Galloping...");
_super.prototype.move.call(this, 45);
};
return Horse;
})(Animal);
var sam = new Snake("Sammy the Python");
var tom = new Horse("Tommy the Palomino");
sam.move();
tom.move(34);