Monday, July 18, 2016

Typescript is what JavaScript should have been

When Brendan Eich added JavaScript to the Netscape browser some 20 years ago, he had roughly a month to do it and, context-wise, JavaScript was added to the Netscape browser as a reaction to the popularity of Java - applets - at that time.

JavaScript was something you could use to interact with the browser via small programs and scripts - think sub 1k lines of code. Unfortunately, that's no longer the situation. It has become common to see 1 million lines JavaScript projects. Projects of this size are unmanageable:

1. Tooling is bad - barely there intellisense, unsafe or no refactoring

2. Development flow sucks - you can't check for common errors until you refresh the page

3. JS Code bases of these sizes are hard to reason about for many reasons like duck typing especially if you got code with bad or lazy naming conventions

This is where Typescript comes in.

1. TypeScript's static typing and annotations are great for catching errors on the tool rather than waiting for a page refresh

2. Code is easier to reason about; example, function params are known if you're using interfaces

3. TypeScript allows safe refactoring and good intellisense support

4. Although from Microsoft, TypeScript is open source with a clear roadmap with rapid releases - typically 3-4 months

But Typescript is not without faults.

1. TypeScript is superset of JavaScript. So any valid JavaScript is also valid TypeScript. In that case, if you write crappy JavaScript, you still get crappy TypeScript. Somewhat fixed if you read and apply Douglas Crockford's advice in his seminal book, JavaScript: The Good Parts

2. Typings. Sort of an edge case problem because some javascript libraries don't have typings thus we don't have intellisense for that library

The good stuff out-weights the bad parts for me. Typescript is what JavaScript should have been.