TypeScript: What & Why?

Rojesh Shrestha
Devnetwork
Published in
4 min readJul 9, 2019
Photo by Patrick Fore on Unsplash

The popularity of Typescript in modern front end web development has been increasing enormously. So, why is everyone talking about it? What is it?

Typescript is a modern age javascript development language which is a statically compiled language to write clear and simple Javascript code. It can be run on Node js or any browser which supports ECMAScript 3 or newer versions.

Actually, Javascript is Typescript. Typescript is a strictly typed superset of Javascript. In fact, anything written in Javascript can be converted to Typescript by simply changing the extension from .js to .ts.

People are generally preferring Typescript over Javascript because of some of the following basic factors.

Readability

Let us take an example of a function in Javascript.

When an argument is passed, it can hold any data type or value; we won’t know for sure what attributes does that argument hold. Hence, we will need to put logs to track down what is being passed from the place where the function is called. This, in essence, is a bit tedious.

Typescript solves that.

In a function in Typescript, we can quickly grasp an understanding of what type of arguments does it take, what type of value does it return, what external data does it require and what it does to them to generate the return value from the IDE itself.

Debugging

TypeScript always points out the compilation errors at the time of development. Because of this, the chance of getting errors at the run-time is very less. Also with strict type checking, the common typos can be easily identified and fixed. Let's take an example from the above function. A minor typo like this could be easily identified during development.

Setup Correct Development Workflow

In Typescript, you first need to think about the type of data you’ll receive, and the type of data you want to produce. Usually, only after that, you sit down for its’ implementation. It also helps in the TDD approach for visualizing and writing test cases with the proper input and output datasets.

Easier Code Review

Typescript gives you another assurance check: the typedef compilation check. If the whole thing compiles and unit tests are there, you can be pretty sure that the whole thing works well. It makes it easier to trust other developers and aids in faster code review.

Refactoring

When refactoring codes, we become little hesitant when it touches many files, for example, renaming components, changing your object, removing deprecated attributes. With Typescript, it aids us in such refactoring by throwing an error whenever our changes have caused any type mismatches in other parts of the code. Also, with some cool IDE command features like “Find All Occurrences” and “Rename Symbol” we don’t need to rely on Search and Replace.

Ease Of Development In A Large Codebase

If you are working on small Javascript codebases, then type checking may not have a that significant impact. But for large codebases with isolated components following the single responsibility principle, it allows developers to expose their APIs with exposed interfaces. They don’t need to know the whole codebase, they can just focus on the exposed API.

We have a clear signature of the imported function. Any changes in it will be easily identifiable where it is being used.

Support

It supports interfaces, sub-interfaces, classes, subclasses, optional parameter function, ES6, JS libraries and API Documentation, giving it upper hand over Javascript.

Obviously, we can see that there are lots of advantages of using Typescript over Javascript. However, it has a bit stiffer learning curve and requires an understanding of how Typescript works to get full benefits out of it. Just because people say its better, it may not be our need. Unless we truly understand why we want/need to move to Typescript, it could also be overkill.

--

--