Advertisment

Go Beyond JavaScript with TypeScript

author-image
Hiren
New Update

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript and is being developed on CodePlex by Microsoft. It can be used as a package with Node.js from the command-line. You can also use it with Visual Studio 2012 by installing the editor plugin to get rich TypeScript tooling. In addition, TypeScript also supports other editors such as Sublime Text, Vi and Emacs. The TypeScript compiler itself is implemented in TypeScript, and can be used in any JavaScript host.

JavaScript not being a full-fledged programming language, TypeScript tries to fill in the missing bits by providing support for features such as static typing, classes, inheritance, etc. As a comparable alternative, Google's approach by adding @param annotations as comments may be considered to be a form of type checking, whereas TypeScript allows you to define the type of the variable in-place in the code itself and accordingly flags potential errors which are technically fine with JavaScript but are semantically undesired.

Advertisment

SNAPSHOT

Applies to: Web developers

USP: Create full-scale applications in JavaScript using TypeScript

Primary link: http://ld2.in/4gi

Search Engine Keywords: JavaScript, TypeScript, Microsoft, open source, CodePlex

On the downside, it is to be kept in mind that TypeScript is not type-safe by definition. TypeScript mainly serves as a development aid while writing JavaScript applications. For instance, while TypeScript will allow you to define classes and let them have private member properties, the same cannot be reflected in JavaScript. But yes, while writing in TypeScript, the code completion offered will be in agreement with the access modifiers of each member of the class that you may have defined, which helps to prevent semantically incorrect JavaScript code. Also, for declarations, TypeScript takes a monolithic approach by relying on a single file lib.d.ts for all declarations. This may not be ideal in all cases. Further, TypeScript also automatically includes all referenced files recursively under a single project. Many developers would not want this. And of course, the project is under heavy development and has many bugs. In fact, the plugin for Visual Studio can be installed without having Visual Studio installed in the first place! You do get the command-line compiler as a result, but for integration with Visual Studio you need to uninstall the plugin and re-install it after installing VS successfully.

Code example

Let's say you need to define a class in TypeScript. You would do it as follows:

Advertisment

class Magazine {

title: string;

constructor(message: string) {

this.title = message;

}

publish() {

return this.title;

}

}

Note the explicit specification of the type of parameter passed to the constuctor. However, the TypeScript compiler will compile this to the corresponding plain JavaScript, which will be:

var Magazine = (function () {

function Magazine(message) {

this.title = message;

}

Magazine.prototype.publish = function () {

return this.title;

};

return Magazine;

})();

Thus, the end result of using TypeScript is the same JavaScript code which you use in your web applications, except that given TypeScript's features, you may now consider JavaScript for full-scale application development rather than just as a scripting language.

Advertisment