Friday 23 November 2012

Thoughts on TypeScript


Well I think I am recovered from the marathon, it was much hillier than I was expecting so it made for some hard uphills and for some fast/fun down hills. Overall it was good fun and the 4 hour drive home was not too bad.

I was looking forward to a week or two of low intensity chilling out. I later find out I will be doing a Taekwondo grading next week so suddenly I am having to practice a lot for that. Hopefully I will be able to chill after that.

And back to geek stuff.

I took a very brief look at TypeScript, this is the Microsoft attempt at improving JavaScript. I haven't written any code just looked.

Google has taken a lot of heat over Dart and while I did not follow peoples reactions to Typescript but I imagine it was similar. If you look at TypeScript it is in many ways a lot more conservative than Dart.

Typescript is a super set of JavaScript so any valid JavaScript is also valid TypeScript. From the name you may be able to guess what it adds. Yep optionally typing. I really like the idea of gradually adding types to areas that have become stable so I am instantly a fan. From the look of it there's quite a bit of type inference going on so you don't have to type out the types.

It has header files, C programmers rejoice! OK these are meant for existing JavaScript libraries that would benefit from having some typing put on top of them before being used in TypeScript. You can of course just use any JavaScript files with having a header - they are optional.

In addition to adding optional typing there have been a few language additions such as proper classes. I think these additions follow the same syntax as what has been proposed for the next version of JavaScript.

Overall I was left with a feeling that they wanted to tread lightly on JavaScript and just add things that will eventually make it into JavaScript or help with larger code bases (typing).

It is a shame that Google did not come out with TypeScript instead of Dart as it would probably gained a lot more acceptance.

Why types can be handy.

I sit on the fence when it comes to statically typed languages (C/C++/D/Haskell) and dynamically type languages (Javascript/Python/Ruby/Lisp). I can see both have good parts and bad parts.

Perhaps the differences are becoming less and less. With better IDE type inference and the advent of optional typing in things like Dart the IDEs used for dynamic languages are catching up with the features of the statically typed languages. It is certainly easier to write re-factoring tools for Java rather than JavaScript and for them to be correct at all times.

With C++ changing the meaning of auto to be Mr Compiler could you figure out this type for me the amount of typing has greatly reduced in at least one main stream statically typed language.

The idea of option typing is very appealing to me. It has been explained else where many times but I shall do so here just for fun.

If JavaScript had the ability to let me at some time add typing to a point class to say only numbers allowed, it would be great. Effectively I would be saying, look this point class is used all over my geometry library so people should only put numbers in it, if not bad things will happen so tell me as early as possible.

I probably would not add this typing when I first created the class but the moment I got a bug where the x component contained a string or an array of numbers then rather annoyed by my mistake the typing would be added. That pretty much sum up why I would like optional typing.

So if I like types why am I currently using JavaScript for stuff. Well programming with a dynamically type language turns out to be fun. There is less verbosity and often less boiler plate code. When I am tired at night this is kind of nice I can just throw stuff together and get sucked in to the code. Dynamically typed languages certainly own the niche of throwing together prototype ideas.

I digress. 

What I have been trying to say is types are nice for reasons other than performance. Catching errors and giving your IDE a better idea of what is going on are good reasons to add optional types even if your language is destined to be compiled down to JavaScript. Yes it has been said better elsewhere.

It is time to stop I this post is starting to feel a little bit meandering and missing whatever the point was I was trying to make.

No comments:

Post a Comment