TypeScript, WTF?!

If you hang around in the JavaScript community, along with left-pad you almost have certainly heard of TypeScript. Having large frameworks, like Angular 2 and EmberJS embrace it have given it a lot of focus. I would like to think too, that Dojo 2 being built on TypeScript adds something to the conversation and I would like to explain in part why you might want to spend time getting to understand TypeScript as well.

Microsoft, Bleurgh

I remember hearing the announcement of TypeScript from Microsoft and immediately dismissed it. I generally did that with most things from Microsoft. They were in my eyes, like a lot of large software enterprises, a good place to crush innovation and focus on brand and marketing. At the time, I was a fairly senior IT manager by day, and closet Dojo committer by night. I had an endless stream of software companies who had solutions to my problems, no matter what my problem was and on the surface, I thought TypeScript was another way to tie enterprises into a Microsoft roadmap.

I was totally wrong.

Fanboy Moment

Part of the understanding of why TypeScript is different, in my opinion, I have to self a bit of fanboyness for a moment. Anders Hejlsberg. As I started to look into TypeScript, I realised that name was vaguely familiar. In the late 80s and early 90s, I was using Turbo Pascal and when Delphi came out, I migrated to it. I loved it and often preached the religion of Delphi to whomever might listen. Direct programming started to become less and less relevant to my day job, but Delphi was my safety language whenever I wanted to hack something together myself. I remember the day when I heard that "big ole mean" Microsoft had "stolen" the core of Delphi from Borland to work for Microsoft.

Of course a few years later, I started hearing about C# and I dismissed it as a "Delphi ripoff" at first, but slowly, it seemed to evolve and actually go beyond what Delphi helped bring to the world. Of course, a large part of C# owes its existence to Anders.

When I made the connection that Anders vision (and code) were large parts of Turbo Pascal, Delphi, C#, and now TypeScript, that made me excited.

It Ain’t Pretty, But It’s All We Got

I don’t really think there are many, if any, JavaScript developers wake up in the morning and say "I am really glad I work with JavaScript, it is such a wonderful language." Brenden Eich often finds himself as chief JavaScript apologist. JavaScript is a perfect example of the Law of Unintended Consequences. It has exploded though, for lots of various reasons. Even with huge detractors and critiques, it is a reality that cannot be denied.

Maybe it could be compared to English, it isn’t a perfect language, it has lots of broken parts, a load of dialects of which only a subset of the language could be considered "globally understood" and many many people don’t really use it all that well. The universal language should have been well thought out and far more rational and organised, but I don’t speak Esperanto, do you? We are not going to get rid of English.

But what if we could add some markup for English, so we could organise our language a bit better? You could take English and optionally apply some rules to it. Maybe something like markdown even, that allows you put things like headings and hyperlinks and code blocks, all the while not breaking the underlying language? Sounds good, well that is TypeScript.

I Don’t Understand

The flexibility and unstructured nature of JavaScript made it "easy" to code, but it never was easy to scale. Scale in large complex maintainable code bases, scale in the ability for people to understand the code, scale in others using your code. The whole way the web works today is through extension and interaction and the biggest roadblock to that is understanding intent.

The first step was to stop writing JavaScript code inline in our HTML markup and breaking it out into separate files we could maintain, creating functions:

function myGreatApi(options) { /* Hopefully no one needs to read this */ }

Ok, that is great, but I would still have to write up some documentation, so someone could use my code. So maybe we could agree on something like JSDoc:

/** * My Great API does something great, just use it * @param {Object} options Some options I need... * @returns {Object} The results of my great API */ function myGreatApi(options) { /* Hopefully no one needs to read this */ }

Ok, that is better, but what if I wanted options to be optional? What if I wanted to express what properties I expected in options ? How do I describe what the return value is a bit better… I could in theory add this all to really complex JSDoc, but that isn’t really maintainable and it isn’t actually enforced (though things like Closure Compiler can give me hints that I might be abusing it). What if there was some way of describing this in a way that could allow someone else just consume this without know a huge amount about it?

interface MyGreatApiOptions { /** * The URL target for my API */ target: string; /** * Translate from one language to another */ translate?: boolean; } interface MyGreatApiResult { /** * The headers returned from the request */ headers: { [header: string]: string }; /** * The response */ response: string; } /** * My Great API does something great, just use it * @param options Some options I need, maybe, if you want */ function myGreatApi(options?: MyGreatApiOptions): MyGreatApiResult { /* now, no one has to read this */ }

Now, not only do I have a completely documented API, it is enforceable at compile time and it many IDEs, code completion is available, so a developer can receive real time insight into how the APIs are consumed.

I have found that TypeScript allows me to not only explain my APIs to others, it helps me structure my code and requires me to remember less about the code I have written, focusing on writing productive code and less time reading and re-reading code I had already written.

Openness

One of the best things about TypeScript is its openness and transparency. It is a first class Open Source project. The team have not only open sourced the TypeScript compiler, they continue to raise the standard and pressure the rest of Microsoft to find ways to open source the tools that they rely open. For example, the tool that builds the browser library definitions has been open sourced. Also, the intelligence that they used to create the in browser editor was released as part of the open sourcing Visual Studio Code.

Hook

Developers are a needy bunch and TypeScript being developed in the open means that people can often express those needs.

Also, sometimes developers confuse open source with democracy. Open source is about being open, allowing people to see your code and your development process and being transparent about what decisions you make and why. It doesn’t mean because you "really need" something that it will be included. TypeScript has articulated their design goals, which is great reference point for making decisions about the direction of the language.

TypeScript doesn’t (yet) have the concept of plugin transformations, which differs it from the likes of Babel. I can see how such functionality could very well satiate needy developers, I can almost see it being a foot-gun for a language that is trying its best to help developers not write "bad" code. Already we are seeing in the Babel community poorly written and maintained transforms that cause all sorts of carnage and problems.

TypeScript is still relatively "young" and therefore it is growing daily, but in my opinion it has largely grown under really good stewardship and in the right directions. There are some significant changes coming in TypeScript 2.0 that continue to deliver on the promise of making JavaScript scale. This includes a whole rewrite of the way types are analysed in the flow of the code and an opt-in feature that starts to deal with logic errors around the flexibility of things being undefined or null in JavaScript.

Given the youth though, TypeScript is still mature beyond its years. By being a superset of JavaScript, it isn’t reinventing the wheel, it is building on top of the language that, for good or bad, powers the web. Coupled with those being involved having a depth of experience in language construction and drawing upon the experience of everyone else by being open has really accelerated it to being ready for production now.

Finally...

So, if you haven’t taken a look at TypeScript, I have hopefully convinced you that it is something to at least worth a bit of your time. I personally believe it is part of a major cultural shift at Microsoft, but even if that isn’t important to your, it can be judged wholly on its own merits. It has some of the best minds focused on making JavaScript scale and the team is going about it in a way that is open and transparent. By embracing the reality of JavaScript and building on top of it, in my opinion TypeScript is transforming the common language of the web, for the better.