To Typescript Or Not To?

Akash Shyam - Jul 27 '21 - - Dev Community

In my previous post, one of my readers, said some very good points against typescript and is resulting in a pretty long conversion in the comments. So, I'd like to do have a final comparison.

Advantages of Typescript

  1. Strict Typing

Once declared, a variable can take only one type and can take only certain values. The compiler alerts developers to type-related mistakes, so they have no opportunity to hit the production phase. This results in less error-prone code and increase in developer productivity.

For example, we call a string specific method on an argument, we can do this without need to add an if check because typescript will through an error if a different type of value is passed.

But static typing is not only about catching bugs. It also gives the code more structure, makes it self-documenting and more readable, speeds up debugging and refactoring.

It’s important to note that TS doesn’t force declaring types everywhere. Developers are free to change the level of type strictness in different parts of the project. This approach distinguishes TS from other statically typed languages and allows you to find the right balance between flexibility and correctness. This also makes it easy to migrate javascript code to typescript because we don't need to convert the entire code base to compile the code.

With TypeScript, everything stays the way it was initially defined. If a variable is declared as a string, it will always be a string and won’t turn into a Boolean. This enhances the likelihood of functions working the way initially intended.

  1. Early spotted bugs

Researchers found that TypeScript detects 15 percent of common bugs at the compile stage. Far from a 100 percent result, this amount is still significant enough to save developers time and let them focus on correcting mistakes in the logic — rather than catching common bugs. Pushing the code through a compiler also decreases the volume of quality assurance and testing activities.

  1. Sweet VS-Code Autocomplete
    Vs code already has great javascript autocomplete but typescript gives it an enormous boost. Also, if we use external 3rd-party libraries, we can check the type definitions which will be faster than checking the docs(some libraries don't have great documentation)

  2. Extra Features
    Over and above the normal javascript features, typescript has several features which are not in javascript. For example, public, private and protected fields, Generics which helps us to create quite complex dynamic typings, Nullish Coalescing (??) etc], abstract classes.

  3. Popularity

Typescript has over 19 Million weekly downloads on NPM:

Screenshot 2021-07-27 at 15.09.56

Next, let's look at the 2020 survey from the state of js, one of the most popular Javascript surveys.

Popularities of Various JS flavours over the years:
![ScreenshotScreenshot 2021-07-27 at 15.17.31

Percent of people who like/dislike typescript:

2021-07-27 at 15.16.31

Experience Over time:

Screenshot 2021-07-27 at 15.21.25

According to the [2020 SO survey], typescript is the second most loved language, surpassing python too! (https://insights.stackoverflow.com/survey/2020):

Screenshot 2021-07-27 at 15.24.45

Of course, these are just numbers. Popularity doesn’t necessarily mean that something is of high quality and should be trusted. Fortunately, TypeScript also enjoys support from some truly formidable players in the tech world.

The language is widely used in software products such as Slack, Medium, Asana, or Visual Studio Code. What’s more, many great JavaScript tools are written in TypeScript, including frameworks (Angular, Ionic etc) and libraries (ZoomCharts, yWorks, or GoJS).

As if that wasn’t enough, tech giants like Microsoft (duh!), JetBrains, eBay, Ericsson, airbnb or Ubisoft are all open about using TypeScript—and who knows how many other enterprise-level companies have included it in their tech stack.

Disadvantages Of Typescript

  1. Extra Stage - Transpiling
    One of the major argument against TypeScript is that it requires compilation, while JavaScript doesn't. But, let’s be honest, most of JavaScript applications these days require a build step. Whether it’s Gulp, Grunt, Webpack, Rollup, Babel, or Closure—a build step is a necessity and nothing really prevents you from expanding it.

  2. Extra code
    To leverage the features of typescript properly, we need to create types, interfaces etc and under a time constraint or a tight deadline, this may cause unnecessary delay. However, in the long run, it will save us a lot of time because a new developer will need a lot less time to understand the codebase.

  3. Learning curve
    Even though typescript is similar to javascript, there is still some learning that needs to be done to understand the code properly.

Conclusion

I think that this is subjective, and as person who loves autocomplete, code readability and syntactic sugar (and a person who introduces a lot of bugs 😅) typescript is the obvious choice for me. However, while working on a tight deadline, vanilla javascript would be better

If you liked this post don't forget to leave a like and follow me here on dev.to and twitter where I will (try) to post javascript/typescript tips and tricks regularly. Bye 🤟

. . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player