What is TypeScript? Why should we use TypeScript?
TypeScript
TypeScript is a superset of the JavaScript language with an open-source compiler. TypeScript is mainly developed and maintained by Microsoft.
The goal of a compiler of TypeScript is to help catch errors early through a type system to make JavaScript more efficient.
Why use TypeScript?
TypeScript has some strong use cases those are discussed below:
Supports modern JavaScript features
Not all browsers and JavaScript runtimes support all features of all ECMAScript standards. TypeScript allows for the use of many of the latest ECMAScript features and translates them to JavaScript.
Strong typed system
The type system of TypeScript is incredibly rich and powerful. It includes interfaces, enums, hybrid types, generics, union/intersection types, access modifiers, and much more.
Allow static typing
JavaScript is dynamically typed. This means JavaScript does not know what type a variable is until it is actually instantiated at run-time. TypeScript adds type support to JavaScript and catches type errors during compilation to JavaScript.
Strick null check
In JavaScript, the runtime errors such as cannot read property 'x' of undefined
or undefined is not a function
are very common errors in JavaScript code. TypeScript reduces the probability of these kinds of errors occurring.
Compilation
TypeScript has a build process to compile JavaScript code. The build process generally takes a few seconds depending on the size of the project. The TypeScript compiler supports incremental compilation so that all subsequent changes can be compiled at greater speed.
Convert to TypeScript
The .js
file can be renamed to a .ts
file and run through the TypeScript compiler to get syntactically the same JavaScript code. If the TypeScript compiler gets compilation errors, it will still produce a .js file. It can even accept .js files as input with the –allowJs flag.
IDE support
TypeScript has a wide range of IDE support. The most popular IDE like Visual Studio Code, WebStorm, Atom, and Sublime has well support for TypeScript.