Why You Should Take A Serious Look At TypeScript

TypeScriptI’m a big fan of Javascript, but I’m happy to admit that it has some flaws. There are classes of programming mistakes that are far too easy to make in Javascript, mainly due to its lax approach to type safety. The way Javascript handles types is the result of decisions made many years ago to make Javascript easy for beginners. Back then, Javascript was a small language with a limited domain of application: the browser.

In the intervening years, Javascript has become the most popular programming language in the world; it is used to build hugely complex front-end applications, not to mention its rapid encroachment on the server with Node. ECMAScript 2015 is a big improvement on the previous version, but there are many more enhancements that could be made to make Javascript more scalable.

That’s what Typescript does. Before I discuss the main benefits of Typescript — its optional type system and the ability to use future Javascript features — it’s important that you understand what Typescript isn’t. Typescript is not a full-on replacement for Javascript like Elm and other “compiles to Javascript languages.”

Typescript is a superset of Javascript, which means that all of traditional Javascript is contained within Typescript. An ordinary Javascript file is valid Typescript. Typescript extends Javascript without making anything that went before obsolete or unusable.

The Type System

If you’ve used C or any other statically typed language before, nothing I’m about to say will be a surprise. Typescript is a typed language with optional type notations. Types allow developers to specify the types of data their code expects, and in specifying those types, the tooling around TypeScript is able to infer how the code is expected to behave (to some degree).

Many developers aren’t fond of being required to add type annotations, so it’s worth emphasizing that they’re entirely optional. Without type annotations, TypeScript can infer types and work much of its magic.

Because of the type system, the TypeScript compiler and other tools, like Microsoft’s excellent Visual Studio Code editor, are able to spot many of the common bugs that bedevil JavaScript developers before the code is put into production.

Future JavaScript

Most JavaScript developers use tools like Babel so that they can use some of the newer features of JavaScript they can’t rely on to be present in the browser. TypeScript provides many of the same benefits and fully implements ECMAScript 2015 as well as other future JavaScript features.

TypeScript can compile to ECMAScript 3 or later, so developers needn’t have any worries about browser compatibility.

One of the reasons I’m so fond of TypeScript is that unlike other compile-to-JavaScript languages, it doesn’t force me to use something entirely new with an uncertain future. CoffeeScript was all the rage a few years ago, and today CoffeeScript codebases are something of an embarrassment.

TypeScript has been widely adopted. Angular 2, Google’s front-end framework is written in TypeScript. You can use React with TypeScript. And of course, Microsoft is fully on-board.

All-in-all, TypeScript is a safe bet if you want the benefits of static typing and something more than plain JavaScript, but don’t want to take the risk with an unproven technology like Elm or PureScript.

Matthew Davis is a technical writer and Linux geek for Future Hosting.

Dedicated Server Special

Take advantage of our Double RAM offer on the E3-1230v2 4 x 3.30GHz+HT server! Only $134.95 per month. Managed and Unmanaged options available at checkout.