Why you should use TypeScript as a frontend developer

If you are a frontend developer, you have surely heard of TypeScript language. Perhaps you are using it in your company or just want to learn it in 2020? In this article, I will give you some reasons why you should use TypeScript instead of vanilla JavaScript.

What is TypeScript?

TypeScript is an open-source language that was released in 2012 by Microsoft and since then it has gained a lot of popularity. TypeScript is a superset of JavaScript that compiles to plain JavaScript. It adds additional functionality to JavaScript such as strong typing, interfaces, classes, modules, generics and annotations. 

Compile-time type checking

JavaScript is a dynamically typed language, which means types could be changed at run-time and there are no compile-time checking in JavaScript. Therefore, an application can compile even if it contains type errors that normally could cause the application from running properly. 

On the other hand, TypeScript provides static typing which enable type checking at compile time. Therefore, bugs could be catched at compile time rather than in run-time. TypeScript will also prevent you from running the application until you solve the errors. 

For example:

let isEmpty: boolean = false;
isEmpty = '123';  // This will give an error.
isEmpty = true;   // This will work.

Readability

TypeScript includes a set of Object Oriented Programming (OOP) features that makes your code easier to read and understand. You could for instance organize your code in classes to improve the quality and maintainability of your code. 

TypeScript:
class Person {
  private name: string;
  
  constructor(private strName: string) {
    this.name = strName;
  } 
  
  getName() {
    return this.name;
  }
}
JavaScript:
const Person = (function() {
  
  function Person(strName) {
    this.name = strName;
  } 
  
  Person.prototype.getName = function() {
    return this.name;
  }
  
  return Person; 
})

Saves you time 

When you write code in JavaScript and create an object or use a method you will ask yourself: “what properties does this object have” or “what arguments does this function accepts”. You have to go through many files to find out the answer. This process is time consuming and it slows you down. 

TypeScript offers a clever Intellisense as you type if you use a supported IDE, like VS code. This saves you time and also prevents errors and typos in our code.  

Summary

Typescript can make JavaScript programming more reliable and easier. It can decrease number of errors and bugs and can increase your productivity as a frontend developer significantly.

Hopefully this article has convinced you to use TypeScript in your next project, perhaps convert your current project to use TypeScript.