TypeScript: The New JavaScript?

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

You find me on Twitter and Google+.
Interested in Eclipse 4 Application Platform trainings?

This Post Has 8 Comments

  1. Dominique De Vito

    There are various JavaScript++ candidates: http://www.jroller.com/dmdevito/entry/in_the_pursuit_of_javascript

    All these candidates include JS + classes + modules + optional typing or type inference.

    On the pro side: it’s nice from a Java programmer point of view. It gives us abstractions we are used to.
    On the con side: it’s all about NIH syndrom, it’s about reinventing the wheel (that is, +/- ActionScript language) as wrote in http://www.jroller.com/dmdevito/entry/in_the_pursuit_of_javascript

    Java is moving towards JS with the addition of closures (+ reimplementing Rhino for speed)
    JS is moving towards Java with the addition of classes, modules and optional typing.
    and
    Java is moving (again) towards the client side with JavaFX
    JS is moving towards the server side with Node.JS for example.

    Well, may be they will met 😉
    http://www.jroller.com/dmdevito/entry/a_false_piece_of_news

    as a side note: PHP sits between JS and Java. And such a JS++ like TypeScript is very close from PHP:
    * JS++ : JavaScript + class-based OOP + optional typing or type inference
    * PHP : class-based OOP + no typing at all (so, it sounds like an untyped Java, with closures)

    I wonder about the future of PHP…

    This being said, while TypeScript and Dart looks like quite close, I rather prefer TypeScript because it offers type inference. And type inference is quite a valuable thing: as typing moves from programmer side to compiler side, it leaves less holes for doing a mistake. It’s fun that a language aimed for mainstream programming is trying to popularize type inference. And it would be much more cool if Java designers (inspired by TypeScript and Scala) are put into the “type inference” boat and take this feature for next Java version.

  2. Anders Forsell

    Coming from an OOP background with Java and C++, Dart is much easier to learn than JavaScript. Their editor (based on Eclipse RCP) is developed by the old Instantiation guys and is really good. And Dart was designed from ground up to be good for developing really large web applications, something that I guess TypeScript will also help with.

    Dart is aiming for a 1.0 release this summer.

    See also discussion on Dart vs AngularJS at http://coder.us1.list-manage.com/track/click?u=0618f6a79d6bb9675f313ceb2&id=39c0597c3a&e=9a6244feba

  3. Michael Vorburger

    Dominique, if you want type inference for a JVM lang very similar to Java, you might like http://www.eclipse.org/xtend?

    Kai, a future blog post from you comparing Dart, CoffeeScript and TypeScript would be very interesting!

  4. Adam

    Thanks, this was really helpful

  5. Dominique De Vito

    @Michael

    I am very fond of Xtend (I was aware of).
    Because, as says the doc, “It’s faster than Groovy [thanks the type inference], simpler than Scala”.
    I am fond of Xtend because it emphasizes what Java could be in the best world.
    I just hope Java designers will take inspiration from Xtend.
    Well, it’s somewhat doable as Xtend is much closer to Java than other Java-derived languages (like Scala for example).

    On the technical side, closure introduction into a given language works smoother when this language uses type inference http://www.jroller.com/dmdevito/entry/facing_the_wall_of_closure
    So, while Java is evolving towards closure adoption, type inference looks like the next/associated logical step.
    Well, there are already some bits of type inference into JDK 8, with closures. I just hope Java designers will go further.

  6. arcseldon

    Thanks the sharing this. I too embarked on an investigation into the various JS offerings, including which compiled language might work best. I enjoy programming in javascript but for large scale apps, it does definitely have its shortcomings, and some of the workarounds aren’t ideal. Having a background in Java, and being used to good IDE support tooling, TypeScript definitely seemed to offer an ideal middle ground. Again, I enjoy hacking with a text editor like GVIM but when you want autocompletion, refactoring etc etc for large scale development this again has its shortcomings. I downloaded “eclipse typescript” plugin (https://github.com/palantir/eclipse-typescript) and got your example above compiling very quickly. The typescript editor is good but you might find the .ts extension causes spell checker to kick in so switch that off and should be good to go.

  7. Drew

    Kai, I reckon TypeScript as a blessing for developers. And as a devoted Eclipse fan, I would also recommend TypEcs as a promising Eclipse plugin (alternative to what arcseldon mentioned above).

Leave a Reply

I accept the Privacy Policy