JavaScript has taken over the world; long live TypeScript!

June 24, 2014 at 12:47 pm Leave a comment

a picture of Darth Vader in front of some other toys

JavaScript has taught me this: if I were an evil overlord whose plans for world domination included getting everyone to download my programming language, I would put it in browsers.

Brendan Eich did just that, in 1995. He put this weird Java-Scheme-Self-HyperCard hybrid of his into browsers, and now all the king’s horses and all the king’s men can’t take it out again. I’m not saying Eich is an evil overlord; I’m saying if he were, this would have been a very clever move indeed on his part. Witness me clapping slowly.

JavaScript is here to stay

Let’s face it. We’re really, really stuck with JavaScript. In two ways, actually.

  • The first, that if you’re a web developer, you have to relate to JavaScript in some way. Because the web runs on JavaScript.
  • The second is Atwood’s Law: “any application that can be written in JavaScript, will eventually be written in JavaScript.”

(Someone may disagree with that last point… but only by failing to notice that nowadays people write office suitesIDEs3D first-person shootersemulators of mostly everything including JavaScript itself in JavaScript!)

Three stages of grief

Once we realize that JavaScript is here to stay, how do we choose to relate to it? We have a couple of options:

  1. Learn the language extremely well, so we can use it well.
  2. Improve the language step by step (without breaking anything).
  3. Wrap the language in a nicer language and use that instead.

Option 1 has become popular in the last few years. The two most earnest efforts I know about to teach JavaScript to the people are JavaScript: the good parts by Douglas Crockford and You Don’t Know JS by Kyle Simpson.

Option 2 is also underway, and seems to be picking up speed. Not only do we have a mostly-complete transition to EcmaScript 5 behind us, but EcmaScript 6 is looking good and more stable every day. Sometimes when the weather is just right, you even hear soft whispers of EcmaScript 7 in the wind. Evergreen browsers are becoming more common and help speed up adoption of newer features. All in all, given that we’re stuck with this language, it feels like we’re doing a very decent job of fixing it.

Option 3… now there’s a Cambrian explosion for you.

I can’t believe it’s not JavaScript

Seriously, check out the list of languages that compile to JavaScript. In that list, you are witnessing what happens when the reality of today’s web locks people into a de-facto development language: people find a way to compile from their own language to JavaScript. (True to form, half of those people geek out and neologize about “transpiling”, while the other half mumbles about kids today and nothing new under the sun, etc.)

On that list, you’ll find existing languages, original languages, languages with interesting ideas, and languages that won’t ever have 10 simultaneous users. There are behemoths like CoffeeScript and GWT on there. Even Perl 6 is on that list. It’s a long list.

Boilerplate

Summer Glau and a tear

Let’s be clear about why that list is so long: JavaScript may be the industry standard, but as a language, it’s clearly lacking in useful abstractions: classes, interfaces, modules. There’s no way to declare the type of a variable. There’s no built-in namespace mechanism, and no way for one JavaScript file to include another. JavaScript is not only unfit for its current role as Overlord language of the web, but laughably unfit. Or, let’s say, we’re almost sure the tears in our eyes are from laughter.

Everyone is helping. The people from option 1 have shown us, across the years, how to emulate classes and interfaces and modules, how to fake it without strong typing, and how to hack the DOM in various ways to emulate features. The result is a lot of so-called “boilerplate” code: a lot of code with a low oomph-per-character ratio. You’re not paid to like it, you’re paid to make it work across all browsers, kiddo.

The people from option 2 are busy putting classes and modules and other nice stuff into your JavaScript. You go, TC39! (You can even run ES6 today, in your browser. It’s pretty cool, but it has to be compiled down to ES5 for now.)

The people from option 3 are basically scouting ahead, seeing what options we have for making JavaScript nicer. That long list of languages is like an ongoing evolutionary experiment in working language designs. What will stick and what won’t.

TypeScript

It’s here that TypeScript comes in. It’s another language that compiles to JavaScript, but compared to the changes many languages on that list make, TypeScript’s are fairly modest, and the language sticks close to JavaScript. In short, TypeScript gives you optional typing and classes, interfaces, and modules/exporting — allowing you to write code with high-level abstractions, while the compiler translates it back to boilerplate.

A bit more in detail:

  • Syntactically, it’s a strict superset of JavaScript, so every JavaScript program is already a valid TypeScript program by definition.
  • It’s almost as if TypeScript’s mission is not to change JavaScript into something it isn’t, but to take what’s already there and describe it on its own terms, allowing us to talk about relations that were always present, implicit in our JavaScript code. I find that rather charming.
  • The boilerplate that comes out the far end of the compiler is described as “how you would have written responsible JavaScript anyway”.
  • What we put in as additional types and classes and interfaces to describe our code, we get back as static type-checking and term completion in our favorite IDE. That kind of safety and convenience is not possible with today’s dynamic-to-the-hilt JavaScript.
  • But what about all the JavaScript library code that’s already out there? Wouldn’t the TypeScript code you’re writing be like a tiny type-safe island in the middle of a vast ocean of untyped JavaScript library code? Yes, but… there’s a community project where people are adding TypeScript types to all the big JavaScript libraries out there, so we can TypeScript while we TypeScript! Gotta love open source.

These are some of the reasons I’m excited about TypeScript. I used to write mid-sized to big projects in pure JavaScript; nowadays, my first choice is TypeScript.

Try it now

You can play around with TypeScript code and the JavaScript it renders in your browser. Oh, and the project hit 1.0 a month ago, which is just what it sounds like: this is ready for production.

One last thing: Edument provides a JavaScript course, which we’re constantly keeping up-to-date to reflect the latest best practices in web development. If the topics in this blog post interest you, consider taking the course, which is two days’ worth of similar material.

 

 

//Carl

Carl Mäsak på Edument

Entry filed under: Courses, General. Tags: .

Video of my invokedynamic talk is online! A few Edument C# Master Class updates

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed



%d bloggers like this: