JAVASCRIPT FOR SQUARES: THE INCREDIBLE RISE OF TYPESCRIPT
JavaScript may be the language of the web, as Jon Udell wrote back in 2015, but it’s a language that forces us to “battl[e] the language itself” as we try to apply it to more complex problems. As Tom Dale, a senior software engineer at LinkedIn and a JavaScript luminary, more colorfully described it to me, “JavaScript is like the hippie parents who let you have premarital sex and smoke weed.” That sounds great, he notes, even liberating, but “eventually you resent that you didn’t have any structure and now you’re 32 and living in their basement.”
Java, by contrast, “is like the super controlling parents who won’t let you drink soda or watch TV.” TypeScript is the happy medium between the two, a fact that has launched it into the top-20 programming languages in a short span of time. While Angular’s uptake accounts for some of TypeScript’s popularity (Angular is written in TypeScript), the real reason has more to do with the dramatic improvements it yields in developer productivity.
All I wanted was a Pepsi
It wasn’t supposed to be this way. JavaScript, as Udell points out, “was never intended for large programs built by teams that use sophisticated tools to manage complex communication among internal modules and external libraries.” Such teams normally default to a strongly typed language like Java, which helps to minimize dumb coding errors. For a variety of reasons, however, we ended up with JavaScript as the lingua franca for web development.
One reason is that Java’s strict control feels a bit inimical to web development. Dale told me in an interview, “Java is like the super controlling parents who won’t let you drink soda or watch TV. Every move is monitored, you can’t make decisions for yourself, and dating is prohibited until after you graduate college. Many people have a breakdown after living with this for so long.” The “parental” intentions may be good, but the impact is decidedly negative.
Caught between the freewheeling, weed-smoking ways of JavaScript and the uptight full-frontal parent mode of Java, some developers have opted for “alt-JavaScripts” like CoffeeScript that purport to fix JavaScript’s deficiencies. Instead, such transpiled languages end up breaking backward compatibility with JavaScript or impose different syntax and semantics, making them difficult to debug.
TypeScript is different. How so? Because TypeScript isn’t interested in rewriting JavaScript. Instead, as Dale has written, “TypeScript is different because of how radically constrained it is.”
Building a better JavaScript
TypeScript, Dale tells me, “is a nice compromise” between JavaScript and Java. “It will guide you, impose some important rules, but ultimately gives you the freedom to break the rules occasionally so you can learn your own lessons.” Otherwise stated, TypeScript offers the benefits of Java while taking advantage of 20 years of improvements in computer science. It’s a big deal.
While not everyone is on board—Facebook, for one, has pushed its own Flow as part of React—the broader community seems to be flocking to TypeScript. Flow, for its part, has largely stagnated compared to TypeScript, which is booming. GitHub ranks TypeScript as the 15th most popular programming language (among 316 contenders). Roughly 10 percent of Stack Overflow’s diverse developer community is using TypeScript now, putting it at number 17 in terms of popularity, a tremendous feat for a relatively new language.
One reason for its popularity is that it’s super easy to learn. After all, if you know JavaScript, you’re well down the path toward knowing JavaScript. “For people who haven’t used TypeScript, it’s easy to imagine that it might be a bunch of weird, complicated new stuff on top of JavaScript, in addition to types,” Dale writes. Not so. “TypeScript is just JavaScript plus the smallest possible set of syntax additions required to let you incrementally typecheck your code.”
Importantly, TypeScript relies on structural typing, not nominal typing. In other words, if you get data into the right shape, it will know it’s the right type. In Java, you might have two objects that you know are exactly the same, but Java doesn’t, so it won’t let you use them together. This is the thing that makes people think Java is inflexible. TypeScript, by contrast, “brings the rigor of types to a language as dynamic and freewheeling as JavaScript,” as Dale styles it.
Clean your room
That incremental typechecking sounds small but isn’t. It pays huge dividends in developer productivity. As Dale notes, “Refactors that used to take weeks take days, sometimes less. And because refactoring is so much easier, cleanup that would never have happened becomes… almost painless.”
There are other reasons for TypeScript’s popularity, among them great tooling, as Narwhal co-founder Victor Savkin calls out. But the biggest reason, again, is that TypeScript makes JavaScript developers dramatically more productive than they otherwise would be. No one really wants to live in their parents’ basement, however freely the weed may flow.