For the lovers of strongly typed languages Javascript has always been a double-edge sword. It offers a great deal of flexibility as it is a dynamic language but that flexibility comes at a high cost, you get no compile time checking so you only find out about potential errors while you are on the browser.

Fortunately (for the strongly-typed language fans out there) there are a range of languages that are a superset of Javascript which can be used to extend Javascript’s functionality making it strongly-typed.

One of such languages is Typescript; Microsoft’s free and open-source input into the world of Javascript superset languages. One of the interesting facts about it is its high adoption from the open source community as it can be incorporated into many different IDEs (such as Eclipse, Webstorm and Visual Studio).

Some of the great features that Typescript provides are:

  1. Compile time checking: Find potential errors on your code before your customer does.
  2. Strongly-typed objects: By specifying the type of objects your functions expect you can reduce the risk of unexpected results.
  3. Classes and inheritance: Typescript provides you with the syntax to declare and use Classes, Inheritance and Modules so you can write more robust code.
  4. Write consistent code: Thanks to TsLint you can create rules for your organization which will ensure your team writes consistent code across projects.
  5. Intellisense: Type faster by letting your IDE provide you with attribute names, functions, etc, something you don’t fully get with dynamic languages.
  6. Javascript is valid Typescript: Do you have any existing Javascript code? no problems !, any valid Javascript code is valid Typescript code.

Below is an example of how a function in Typescript would look like (note how it can be specified what type of object you function expects and what type of object it will return):

[sourcecode language=”javascript”]
export function parseJsonDate(jsonDateString: string): string {

return jsonDateString ? new Date(parseInt(jsonDateString.replace(“/Date(“, “”), 10)).toDateString() : “”;
};
[/sourcecode]

 

Fore more information or to play around with Typescript be sure to visit the TypeScript website

 

Have you had any experience with TypeScript? Do you think it provides any other benefits apart from what was outlined above?