Friday, May 16, 2014

Dart vs TypeScript

Coming primarily from a C# background (as well as C, C++, & Java), working with JavaScript was quite a paradigm shift.  As a result, I was very slow developing in JavaScript because I had to learn all of the ins and outs of the language while I was trying to implement functionality.

As you may know from reading previous posts, this led me to switch to Dart.  Having a syntax very similar to C#/Java, I was more than happy to dive in.  I quickly developed a simplistic game engine/framework and even developed four games in one month (one game a week).

Having changed jobs (again!), I took time off of my hobby development to focus on getting up to speed at my new job.  I'm now at the point of where I'm investigating the feasibility of a new idea.  I want to turn my framework into a library that can be used by any web developer to make games.  I was envisioning compiling the Dart code into JavaScript, allowing other developers to interface with it and even create new scripts on the fly.

My entire Dart world shattered apart when I discovered that this would be incredibly difficult, if not impossible to do.  You see, even though Dart compiles to JavaScript, it was not meant to be consumed by any one else in that form.  The focus is squarely on the Dart VM which isn't even implemented in any real browser.  In order to interface with JavaScript, you have to use the interop layer.  Yikes!

I sat for several days trying to plan out the best way of exposing practically everything from my Dart code to JavaScript as well as allow random JavaScript code to be serialized along with Dart so that everything was saved together as one game.  I eventually decided to look elsewhere for other options.

The major alternatives to Dart are:
JavaScript - I've obviously already tried that and need to get away from it until JavaScript 2.0.
CoffeeScript - This has one of the worst syntaxes I've ever seen.  In my opinion, it makes JavaScript worse.
TypeScript - Everything I've seen thus far for this is amazing, but I'm hesitant after Dart.

Dart and TypeScript both have:
  • Types
  • Generics
  • Classes
  • Properties (getters and setters)
  • Static Fields & Methods
  • Aliasing
  • Optional & Default Parameters
  • Lambda Expressions (Anonymous Methods)
What TypeScript has that Dart doesn't:
  • Enumerations
  • Interfaces (explicit)
    • Dart only has implicit interfaces via classes
  • Modules
    • Dart uses libraries instead
  • public/private keywords
    • Dart uses underscores to mark fields/methods as private.  BLEH!!!
  • Rest Parameters
  • Overloaded Methods
    • I can't believe Dart doesn't support this.  This is huge to me.
  • Generic Constraints
  • Superset of JavaScript
    • All existing JavaScript is valid TypeScript
    •  This allows direct interfacing with JavaScript code
    • This also means that its implementations of classes, modules, etc are JavaScript 2.0 compatible!
What Dart has that TypeScript doesn't:
  • Pub
    • A great way to get other people's packages automatically
  • Basic Framework Classes
    • Lists, WebAudio, WebGL, TypedData, etc
    • TypeScript has lib.d.ts, but it doesn't provide as much
  • Abstract Classes
  • Operator Overloading
    • Very handy feature that I hope TypeScript gets at some point
  • Indexers (overloading [])
    • Very useful for creating custom collections or math types (Vector, Matrix, etc)
One of the biggest advantages of TypeScript over Dart is this:

Cloud 9 supports TypeScript

I haven't written a single line of TypeScript (yet!), so I can't truly comment how if it is better than Dart, but on paper it looks fantastic.  I'll try it out soon!