Why use Typescript over Javascript?

The goal of TypeScript is to help catch mistakes early through a type system and to make JavaScript development more efficient.

Why use Typescript over Javascript?

The complexity of the JavaScript project codebase tends to grow speedily with each new code line, making it extremely tough to maintain on the large codebase.

The rapid increase in the popularity of JavaScript on the backend made things worse, as usually the backend is much more complex than the frontend. That’s when Microsoft created TypeScript to solve this issue.

TypeScript does similar to javascript, what LESS or SCSS does for CSS. They are supersets of it, which means that every JavaScript code you write is valid TypeScript code.

Plus you can use the other benefits that it adds to the development, and the transpiled code will be valid JS code. You can even set the JavaScript version that you want your resulting code on.

Lets take a look at a short TypeScript example:

type Params = {
   first: number;
   second?: number;
}
const func = ({first, second}: Params ) => first + (second ?? 0);
const result = func({first: 1, second: 3});
console.log(`The result is ${result}`);

It compiles to pretty straightforward JavaScript code:

"use strict";
const func = ({ first, second }) => first + (second !== null && second !== void 0 ? second : 0);
const result = func({ first: 1, second: 3 });
console.log(`The result is ${result}`);

types got removed, and the null-coalescing operator got replaced with the null check in javascript.

We’ve declared a type for parameters of our function to restrict possible parameters to objects that should always have the first element that is a number and optionally has the second element that is also a number or (optional)undefined. if we try to pass anything else like String would give a compilation error.

Great tooling support with IntelliSense

One of the biggest benefits of TypeScript is its code completion and IntelliSense. Intellisense provides active hints as code is added. Since itself is written in TypeScript as well, editors can present all the methods available and what they expect as arguments. All the best IDE’s available today have support for code completion, including VScode, WebStorm, Sublime Text, and others.

Optional Static Typing

The optional static type system that TypeScript provides is probably the first thing that comes to mind when you think of it. Variables, functions, properties, and other objects can all have types. This will assist the compiler in displaying warnings about any potential code issues before an app is executed. Types are also useful when using libraries and frameworks since they inform developers about the types of data that APIs expect. The most important aspect of the type system to remember is that it is optional. TypeScript does not compel developers to include types that they do not wish. However, as a program grows in size and complexity, types can give significant benefits.

Some facts about adoption and popularity:

In the 2017 StackOverflow developer survey, TypeScript was the most popular JavaScript transpiler (9th place overall) and won third place in the most loved programming language category.

In the 2020 StackOverflow developer survey TypeScript was the second most loved technology.