HTML5 Zone is brought to you in partnership with:

Kai is a DZone MVB and is not an employee of DZone and has posted 18 posts at DZone. View Full User Profile

TypeScript: The New JavaScript?

02.28.2013
| 3357 views |
  • submit to reddit

In the last year I did quite a lot of evaluation, prototyping and testing in the HTML5/Web/JavaScript space. For me, as a Java veteran it was (and still is) a quite refreshing experience and a lot of fun. One question I wanted to answer for myself was: Is it possible in a big organization like Siemens to do professional software development using JavaScript + HTML5? My current answer would be: YES, but with a lot of coaching from experienced people who know the best practices regarding language, project structure, module systems, testing, libraries, tools etc. During my activities I took a look at Dart, CoffeeScript and TypeScript.

I have to admit, I like TypeScript a lot. It gives me most of the abstractions that I am used too from my background as Java developer, like classes, modules and interfaces. And I also like the optional strong typing during compile time with corresponding IntelliSense (or “Content Assist” in EclipseSpeak). The idea of TypeScript is to enhance JavaScript by providing these additional abstractions and compile to plain ole JavaScript.

Here a TypeScript example:

class Student {
  fullname : string;
  constructor(public firstname, public middleinitial, public lastname) {
    this.fullname = firstname + " " + middleinitial + " " + lastname;
  }
}
 
interface Person {
  firstname: string;
  lastname: string;
}
 
function greeter(person : Person) {
  return "Hello, " + person.firstname + " " + person.lastname;
}
 
var user = new Student("Kai", "U.", "Toedter");

compiles to the JavaScript

var Student = (function () {
 function Student(firstname, middleinitial, lastname) {
   this.firstname = firstname;
   this.middleinitial = middleinitial;
   this.lastname = lastname;
   this.fullname = firstname + " " + middleinitial + " " + lastname;
 }
 return Student;
})();
 
function greeter(person) {
  return "Hello, " + person.firstname + " " + person.lastname;
}
 
var user = new Student("Kai", "U.", "Toedter");

TypeScript was created by Microsoft (Anders Hejlsberg) and is licensed under Apache 2.0. The main Web resource is typescriptlang.org. Here you find all information and you can play around live with a nice editor (seeing the original TypeScript and the compiled JavaScript). Currently the TypeScript compiler runs either as a node.js module or using Windows Scripting host. As it is from Microsoft, no one will be surprised that Visual Studio actually has the best IDE support for TypeScript. But the current RC release of JetBrain’s WebStorm also has TypeScript support.

There is no Eclipse-Plugin so far. Recently I tried to write one using Rhino but I got stuck and gave up. But probably people from the Eclipse community who have more experience with integrating editors, content assist etc. might want to give TypeScript a try :)

What do you think about TypeScript?

Have Fun!

Kai



Published at DZone with permission of Kai Tödter, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)