TypeScript: Advantages & Disadvantages

First of all, What is TypeScript?

The textbook definition is, TypeScript is a superset of the JavaScript language that has a single open-source compiler and is developed mainly by a single vendor: Microsoft. The goal of TypeScript is to help catch mistakes early through a type system and to make JavaScript development more efficient.

So what does it mean?

For the simplest example we have a function that takes a string and two numbers to return that string with sum of those numbers in a string format.

const foo = (text, num1, num2) => {
  return `${text} ${num1 + num2}`;
}

We define the types of the parameters of that function

const foo = (text: string, num1: number, num2: number) => {
  return `${text} ${num1 + num2}`;
}

So later when you try to call the function with wrong parameter types 

foo(1, "bar”, 2);

It doesn’t compile and lets you know “text” should be a string and “num1” should be a number.

Sounds cool and simple doesn’t it? Well… Yes and no 🙂

I agree totally on the cool part but this was an extremely simple example. There are much more advanced cases with different syntaxes. Maybe we will look into them in the future articles…

Also keep in mind that, when you are creating a quick prototype involving TypeScript would cost you time, for such quick thought I suggest to go without. But make sure while creating the final code, use TypeScript.

For now, let’s focus on pros and cons.

Cons:

1- Complicated typing system

2- False sense of security (just because TS doesn’t give a warning, it doesn’t mean your code is 100% practical and working)

3- Requires compilation, but I am just adding this here because I saw it on an article online. Nowadays most frameworks require some compilations. Applications with pure JS is very rare. We all use some kind of compilers like Webpack, Babel, Gulp …etc.

4- Can cause problems with libraries written with other systems like Flow. But there are @type libraries supported by the community to provide quick solutions.

Pros:

1- It prevents mistakes that can be overlooked by compilers caused by type incompatibility

2- Much fewer UI errors

3- Increases team performance and helps new joiners to shorten the adaptation period. Because all team members can easily see which props a component or a function needs to execute correctly, what is optional what is mandatory without reading documentation or following the code line by line.

4- It is great to have in CV considering popularity of TS is increasing day by day among industry.

Conclusion:

To sum up, I think when we weigh pros and cons I believe TypeScript is a great addition to any project. I define TypeScript as “TypeScript protects developers from themselves and other developers”.

Let me know what are your thoughts!

Blog post by Oz

Leave a Reply

Your email address will not be published. Required fields are marked *