What is TypeScript?

  • TypeScript
  • JavaScript

JavaScript is the de facto programming language required for front-end development. It has been around since 1995 and has kept evolving. After Node.js launched in 2009, we were able to use JavaScript for backend and general programming outside of the web browser. These days a version of almost every type of software runs on the web, meaning everyone needs to use JavaScript.

What’s the problem?

Here’s the issue, the larger your JS codebase gets, the messier it is, making maintenance and reusability of the codebase quite complicated. There’s also the issue of it not having much support for Object Oriented Programming. This is one of the first concepts a programmer learns but Classes were added to JavaScript only recently. One of the most important features for maintainability and reusability of code, type checking is also not available.

Note, it is possible to use some OOPs concepts in JavaScript but all of them require some kind of work-around to actually work.

In comes TypeScript

TypeScript was created by Anders Hejlsberg (designer of C#) at Microsoft, to combat all these issues with JavaScript while still maintaining the integrity of the language itself. TypeScript is a typed superset of JavaScript compiled to JavaScript. This means that we don’t actually run TypeScript, it needs to be compiled into JavaScript which is then run on top of Node or the browser. We just don’t need to construct our own hacks into JavaScript to support basic concepts like interfaces, classes or type checks for that matter.

For comparison, let’s create a class called Book.

JavaScript

TypeScript

When we compile the TypeScript code, we will get something that looks like the JavaScript code written above. But when we use the code, we will see the difference.

// calling constructor without parameters will work perfectly fine                // in JavaScript, calling .read() will throw runtime error in   
// console because this.pages will be undefined.  
let book = new Book();

But TypeScript will tell you when you’ve made errors like this.

This was a very small example. Imagine having hundreds of classes like this to work with daily. To make use of everything effectively the programmer needs to read the entire source code. But with TypeScript we can reuse the parts of the code we need without having to worry about making these simple errors, provided the type annotations are done correctly.

Find out more about TypeScript here https://www.typescriptlang.org/