Choosing BetweenTypeScript vs JavaScript: Technology, Popularity

Choosing BetweenTypeScript vs JavaScript: Technology, Popularity

TypeScript is a relative newcomer to the JavaScript field, and is a promising addition to JavaScript programming. The value proposition is adding some rigor, type checking and other compile-time checks, that have been lacking in JavaScript. Checks like these should help us write more robust code, especially in large applications.

This is an excerpt from Quick Start to using Typescript in Node.js applications

The loosey-goosey nature of JavaScript programming is great for fast coding, but in theory it does not work so well for larger applications. In a small application a single programmer can easily review the code to find coding mistakes, but in a huge app developed by a large team manually finding coding mistakes is much harder. A common problem is to define an anonymous object that is used in a set of cooperating functions, then later changing one of the functions without changing the others.

In many languages the compiler plays a role in catching certain classes of coding mistakes. Going by the example just named, in other languages not only will the compiler catch that mistake, the IDE has refactoring capabilities to actively help programmers ensure type changes propagate to all affected code.

As JavaScript is being used for larger and larger applications, more rigor should be A Good Thing.

TypeScript is rapidly gaining popularity. Therefore — Is TypeScript gaining popularity? Is JavaScript losing popularity? Are programmers happy with what TypeScript offers? Are JavaScript developers looking for more rigor in their code?

Theory: Better programs through compile-time checks

Why is TypeScript important? Why not just use JavaScript and get on with things? It has to do with our ability to write large applications in JavaScript.

Originally JavaScript was designed for small simple code snippets on web pages, and to help Java Applets better integrate with web pages. The name, JavaScript, was because of a marketing agreement between NetScape and Sun Microsystems where JavaScript would be a scripting language for Java Applets. In a small code snippet any competent programmer can easily inspect the code to see what it does, and to catch errors by eyeball.

For the last several years browser-based applications written with JavaScript have grown ever-more complex. Plus there’s this little thing called Node.js giving us the chance to write server-side code with JavaScript, and with it folks are writing large scale server-side applications. In a large application it is, in theory, very easy for the left hand to not know what the right hand is doing, meaning a change in one module could screw up assumptions made in another module and send the application crashing into a pile of bits scattered all over the floor.

In theory, enterprise-grade systems need the rigor one gets with strictly typed languages like Java or C# or C++.

In theory, in small applications strict type/code checking is not critical because anonymous objects with loosely typed fields is manageable in small programs.

In theory, a software system with millions of lines of code can only work if the compiler and runtime system help programmers find bugs.

In theory, a language with strong typing and other features can prevent certain classes of programming errors.

That’s the niche occupied by TypeScript, namely a programming language with enough strictness to keep programmers from shooting themselves in the foot, but with the ease of use of JavaScript. Code generated by the TypeScript compiler is deployable to modern (ES5 and beyond) JavaScript environments (web browsers and Node.js) so it can bring rigor to the ever-more-complex applications being built on JavaScript platforms..

Popularity Contest — JavaScript vs. TypeScript

As a software engineer how do you tell which technology to use? There is the technical assessment — does the technology do what it says, and is it a fit for your projects? There’s also a looser evaluation of whether the technology has “legs” meaning will it be in use for the next 10 years or more?

TypeScript has solid technical merits. It adds many useful features to JavaScript, as you might expect since the lead engineer was behind a string of popular programming languages from Delphi to C#. It appears to be a very good solution for teams looking to add rigor to large JavaScript projects. For now we’ll assume TypeScript has the technical merits. Farther down this article we’ll go over some blog posts that give more data on the technical merits.

How popular is TypeScript?

Let’s start with Google Trends — which shows us search query trends. The graphs which follow are set to summarize Worldwide search results, and cover the last five years before May 7, 2019.

TypeScript for the last five years — Source: Google Trends
JavaScript for the last five years — Source: Google Trends
JavaScript versus TypeScript for the last five years — Source: Google Trends

Just from this little bit of data we see that interest in JavaScript is far stronger than for TypeScript. JavaScript interest is at best stagnant, or perhaps declining slightly, while TypeScript interest is growing strongly.

Because correlation is not causation we haven’t proven anything other than TypeScript interest is growing.

One strong source for the interest in TypeScript is that Angular 2 uses TypeScript. On the Google Trends page it shows related queries, and the combination “angular2 typescript” is very popular.

TypeScript (blue), Node.js (yellow), TypeScript+Node.js (red) — Source: Google Trends

Given that Node.js is often used for large applications, it’s interesting that the combination of Node.js and TypeScript is not showing much search interest. Also curious is the stagnant/declining interest in Node.js.

Express.js (blue) TypeScript (red) — Source: Google Trends

Similarly if we pick Express.js, one of the popular app frameworks for Node.js, we see a similar pattern. Express.js interest is at best stagnant, while TypeScript interest is taking off.

What did we prove? TypeScript interest is taking off, while other tools are at best stagnant.

The Dice Skills Center gives a different view into relative popularity of different technologies.

JavaScript popularity — Source: Dice Skills Center

Like with Google Trends, this shows JavaScript popularity as strong (unsurprisingly) but stagnant or declining.

TypeScript popularity — Source: Dice Skills Center

Like with Google Trends, TypeScript popularity is growing strongly.

Node.js popularity — Source: Dice Skills Center

And where Google Trends showed Node.js interest leveling off, this shows it growing strongly — and beginning to level off in 2018. In the Google Trends chart, Node.js interest over the same period was more stagnant than this.

The difference is that the Dice Skills graph shows the number of job listings requiring certain skills, versus Google Trends showing search queries.

Wouldn’t it have been cool to graph the trends of languages used in Github projects, or trends of StackOverflow questions? Unfortunately I could not find a source of such graphs.

What are other programmers saying?

Another part of evaluating a technology is to look for blog posts about that technology. Are other programmers complaining about the tool, or praising it to the heavens? And what do they say about the technical merit of different technology choices?

I’ll start with one of my own articles

For over 10 years I worked in the Java team at Sun Microsystems, which meant I was fully steeped in Java for this and Java for that. As a blogger on java.net, I strongly defended Java against folks pushing JavaScript. But after leaving Sun, about 10 years ago, I discovered Node.js and have completely switched to programming in JavaScript, and have written several books on Node.js programming. Bottom line is that a couple years of developing web applications with Spring left me certain that the Java ecosystem is buried under many layers of bloat, and that JavaScript with Node.js is a breath of fresh air. The article has been very popular, and folks are weighing in either with how article is “right on” or else that I’m completely confused and should never have given up on Java. Some of the responses suggested that TypeScript was a great middle ground between these approaches.

I’ve done a bit of TypeScript coding since then, and have mixed feelings. It is nice to have the compiler catch some of the errors. But since it does not support run-time type checking, there’s a lot of potential for error.

This article quotes many leading tech industry commentators who are saying things like:

  • “TypeScript is slowly becoming the new and improved JavaScript,” — TIOBE
  • “TypeScript is a strictly typed superset of JavaScript, so it’s great to see TypeScript’s adoption rising as it helps address the needs of JavaScript developers that want a bit more type safety and insight into potential issues in their application at compile time as opposed to runtime.” — Kris Borchers, JS Foundation

This article by Eric Elliot makes the case that while static types are very useful, they aren’t a solution for catching bugs. He says code quality studies of a bunch of open source code repositories show that “bug density” does not improve for languages that use static types. Instead what helps reduce bug density is “TDD” or Test Driven Development.

This study examined several open source projects, looking at bugs found in the project, and then inserted type definitions to see if TypeScript would have found the bug at compile time. They found many instances where the compiler would have caught the bugs.

Any professional software engineer knows that the earlier a bug is detected the cheaper it is to fix the bug. A bug caught at compile time is the cheapest to fix.

This article demonstrates how useful TypeScript is at describing types and object contents. It’s all looking very good until just before the conclusion.

Drawbacks stated in the article:

  • Compilation — The developer workflow is a little more complicated because they now have to compile their code.
  • Debugging — Using a debugger with code generated by TypeScript is not straightforward.
This is a highly opinionated talk by Ryan Dahl, the inventor of Node.js, about Node.js and the things he regrets about Node.js. Those points are interesting, but towards the end he describes a new project:

It is a new programming runtime built on V8, using Rust instead of C++, and users of Deno write their code in TypeScript. In the talk above, Dahl talks very glowingly about TypeScript, and his preference for it is evidenced by having users of the Deno runtime writing their code in TypeScript.

This article was written by an engineer seemingly coming from a C#/.NET background. The similarities between TypeScript and C# were therefore welcomed. He had a team of C# developers come on board who were able to quickly get going with TypeScript.

The article names a few downsides including the compilation and debugging issues listed earlier.

Embrace/Extend/Extinguish

The TypeScript project has all the appearances of being a properly executed open source project for which there is a ton of excitement. But we have to raise an issue from Microsoft’s past, one that was alluded to in Eric Elliots blog post linked earlier.

Microsoft has not always been a good citizen of the open source movement. For most of its history the company was extremely negative to open source efforts, and to those of us who came of age in the 1980’s-1990’s Microsoft was the enemy.

Microsoft’s hallmark strategy was Embrace, or to bring in new technologies, Extend, or to add features to the technologies it brings in, and Extinguish, or to make the original technology irrelevant by dint of popularizing the incompatible version.

Looked at through one lens, TypeScript is in the Extend phase of that strategy.

This consideration is important in the question of whether to adopt TypeScript. By adopting TypeScript are you feeding a progression of events which results in JavaScript becoming irrelevant? We have no idea if that’s Microsoft’s plan, but are simply pointing out that’s what Old Microsoft would be planning.

Old Microsoft and New Microsoft are different animals. New Microsoft is very open to open source projects, having launched or contributed to many of them. For example Node.js got a big boost when Microsoft poured a lot of engineering expertise into making sure it ran well on Windows. And then there’s the Visual Studio Code IDE, another open source project, that has become extremely popular.

Old Microsoft would have done none of those things. Let’s hope that TypeScript is one of the hallmark projects of the New Microsoft.

For example Microsoft could promote features developed for TypeScript to the ECMAScript committee to be made a standard part of the language.

Conclusion

The question we started with is to note that TypeScript is getting pretty popular, so should we consider adopting it in our own projects. We suggested the evaluation should include technical meritpopularity, and the future of the project. We’ve found and summarized a lot of information, so let’s try to bring it together into a few bullet points.

  • TypeScript has useful features layered on top of JavaScript, and it is a strict syntactical superset of JavaScript.
  • The type checking and other features are widely recognized as being very useful, especially in larger projects.
  • Some claim compile time type checking helps find bugs, while others claim it does not. As a matter of experience, any programmer who’s used strict type checking compilers know the compiler detects certain classes of mistakes.
  • Since TypeScript does not do runtime type checking, the benefit of switching to TypeScript is lessened.
  • TypeScript popularity is growing rapidly where other technologies in the JavaScript ecosystem are stagnating or declining.
  • The TypeScript project has the backing of a large company, Microsoft.
  • The TypeScript language is the brainchild of an engineer with a strong track record of developing incredibly useful programming platforms.
  • The TypeScript language shares familiarity with Java, C# and other languages making it easier to learn.

David Herron is an independent software developer and writer living in Silicon Valley. He has written several books about Node.js programming, writes about technology on TechSparx.com, and about electric vehicles onGreenTransportation.info.

Source: itnext