What is TypeScript and why should I care?

TypeScript is a programming language that builds on top of JavaScript. It’s not a complete language, but rather a layer that adds new features to JavaScript. The foremost of these is obviously strong typing, where the name is derived from.

Programming languages can be classified into two groups according to their typing methods. They are either strong typed or loosely typed. In a loosely (weakly) typed language, like JavaScript, individual variables can simply be declared. In strongly typed languages, like Java, developers need to implicitly state the type of the variable upon declaration.

In general you could say that it’s more laboursome to write in strongly typed languages, but the effort pays of in ease of maintenance and potentially increased performance. This all boils down to the fact that the intent of the programmer is made more clear to the computer.

Take for that you have variables “name” and “age” declared as string and integer respectively, then the it’s clear that age needs to be a number (15) and not a string (fifteen). Using a wrong type of value will result in an immediate failure upon compilation. In a weakly typed language your code could take a string for a number, but this will not cause the program to fail immediately. Rather it will fail later when you would, for example like to perform math on the age.

The above means that there are potentially more hidden errors in loosely typed code. Many popular web programming languages like JavaScript and PHP are weakly typed. This is probably one of the key reasons for their success as it lowers the barrier of entry for beginners. Both languages are now over two decades old and are used for creating software orders of magnitude more complex than originally intended.

PHP got strong optional strong typing in PHP 7.0 (PHP 7.0: Why Scalar type hints with strict mode, and stricter weak mode), but for JavaScript strong typing is not yet in the horizon. And even if it were, due to targeting the browser itself, it will take a long time for new JavaScript features to be widely usable everywhere.

TypeScript adds optional strong typing to JavaScript

TypeScript is an open source project that adds optional strong typing to JavaScript. It adds this and other advanced language features like interfaces and generics to JavaScript. All of this is done in the development environment and the resulting output is regular human readable JavaScript. This means that TypeScript can target billions of browsers and other JavaScript execution environments.

ECMAScript is the standard that defines JavaScript, which in turn is the base that TypeScript is built on. Unlike many other compile-to-JavaScript languages like CoffeeScript it does not create a whole new language with it’s own syntax. This lowers the barrier of entry for JavaScript developers as any existing JavaScript is already valid TypeScript.

TypeScript does not enforce strong typing, instead it can be taken into use gradually. Infact you don’t even need to define the variable types implicitly as TypeScript assumes the type of a variable based on the first set value through a feature known as Type Inference. In addition TypeScript type definitions can be distributed as a separate file and there are already many high quality type definitions for TypeScript for popular JavaScript tools like jQuery, Angular.js (1x.), Google Maps ja CKEditor.

TypeScript does not generate true strongly typed code as anything related to them are removed at compile time. If strong typing some day comes to JavaScript, then the TypeScript compiler can be updated to target strongly typed native JavaScript. This is a common practise in contemporary JavaScript development, known as transpilation — it means translating from source code to source and not executable machine code as in regular compilation.

In a nutshell TypeScript is an open definition for a programming language and a collection of open source tooling to emulate strong typing during development. This leads to significant advantage when working with large codebases, but is also advantageous for smaller projects as strong typing enables better software development tooling.

TypeScript is already widely deployed

TypeScript shines when working with complex software projects. Maintenance and development of critical systems with the tooling that TypeScript provides is superior to that of vanilla JavaScript. Refactoring existing JavaScript code with confidence instead of resorting to black magic and voodoo. With the now-ubiquitous nature of JavaScript TypeScript can be used for both server side and clientside development.

TypeScript was originally launched back in 2012, but during 2016 the language has seen increased momentum. Many well known companies like Basware, Epic, Ericsson and Wix have adopted TypeScript. But what is even more remarkable is the popularity it has gathered among framework projects. Many top-of-mind JavaScript frameworks like Angular 2, Aurelia, Dojo, Ember, Ionic, NativeScript are now written in TypeScript.

Frameworks provide developers a platform to build applications on top of. This means that as a target audience framework developers set the bar very high. The original development team behind JavaScript also worked on Microsoft’s .NET platform before. It seems like this background and experience in building programming languages and associated tools continue to be of high value.

TypeScript is a tool worth exploring for every software developer. It’s stable, open source and has backing from large corporations like Google and Microsoft. Due to it’s nature it offers interesting opportunities like transpiling Java code to TypeScript code. It’s instantly familiar to PHP, .NET and JavaScript developers and easy to approach for beginners.

Learn more about TypeScript:

Originally published at www.symfony.fi.