TypeScript starts where JavaScript leaves off

As a software engineer, I learned that one measure of my success is whether or not the code I’ve written is in production and maintainable by others years after I moved on to other things. Self-documentation features of the programming language play a crucial role.

The rapid adoption of JavaScript and Node.js in the enterprise revealed some flaws in the platform. While it is possible for apps to be rapidly built and put in production, the long term maintainability becomes almost impossible. One of the problems with JavaScript is that the code is not self-documenting.

Consider object-orientation as an example: class keyword was only introduced in 2015. Until then the mechanism to accomplish this was via prototype functions. Class keyword is a syntactic sugar over prototypes, but it sure is far more readable in the longer term than a prototype function.

Another example of poor self-documentation is lack of strong typing. Strong typing is a programming language concept that guarantees consistency of the code at compile-time. It is possible in JavaScript to do the following:


var amount = 10.52;
amount = amount + 1.57;
amount = "$" + amount;

TypeScript addresses these and many other flaws of JavaScript that prevent it from becoming a real enterprise application platform. Syntactically TypeScript is a superset of JavaScript. Since TypeScript cannot be natively executed by Node.js or by browsers, it needs to be compiled into JavaScript – the process sometimes is also called transpiling.

When computers made their way into the enterprise in the 1960s and 1970s, the coders had to write programs in assembler – a low-level mnemonic language that is translated directly into machine code. Believe it or not, many of these programs are still around in banks, government, and other big enterprises.

As you can imagine, assembler programs are difficult to maintain at scale. This is where languages like C, PL/I, Pascal, COBOL, and later C++, Objective-C, and Java come in. Programs written in these higher level languages are first translated into assembler as part of compilation. During the compilation stage, the compiler can identify silly programming mistakes and prevent difficult to solve problems later on.

In a way, JavaScript is the assembler and TypeScript is the higher-level language on top of it. TypeScript offers strong typing and syntactic mechanisms for object-orientation and name spacing similar to languages like Pascal, Delphi, Java, and C#. In fact, Anders Hejlsberg, the lead architect of TypeScript, also worked on both Delphi and C#.

Over time, it is possible that the features of TypeScript may find their way to JavaScript. That would be nice. Until then, TypeScript helps us keep our sanity when using Node.js in the enterprise.

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