📜 ⬆️ ⬇️

Thinking out loud about TypeScript

Some time has passed since I first met and made friends with TypeScript. In those days, the version has not yet exceeded one . A recently released release 1.7 . During this time, we have become accustomed to each other and have experienced many successes and disappointments. I would like to share a bit of my impressions and thoughts about this JavaScript dialect as an independent language. The idea of ​​such a post came to me spontaneously when discussing the next holivar with my colleagues.

So, what is actually TypeScript - probably already not a secret to anyone. But still, I want to mention that this is an attempt by Microsoft to bring static typing to JavaScript. Examples of code and tasks that it allows to solve, you can look at the official website or here on Habré , the benefit of the articles written is not enough. On Habré there is already an article of this kind of TypeScript: general impressions , therefore, in order not to repeat, I decided to highlight the pros and cons of working with the language, based on my personal experience. Recall and list the pros and cons of the language was quite difficult.


Language popularity


The TypeScript project was created by Microsoft. In fact, its creator is Anners Hejlsberg . Almost from the very beginning TypeScript began to quickly gain popularity due to its flexibility and performance. Quite a few projects that were written in JavaScript began to be transferred to TypeScript. The growth of interest in this language is also caused by the fact that a number of ideas that are implemented in it later became part of the new JavaScript standard. More and more projects are being rewritten in this language, including major projects such as Angular 2.0 and vscode .
')

Quotes of reputable people


TypeScript is probably one of the best JavaScript languages ​​on the front end. The code that it generates looks the most attractive. And I think that it is able to take the load off the ECMAScript standard on the implementation of such things as declarations and classes. Anders showed that this functionality is well supported by the preprocessor, so there is no need to change the main language.

I believe that free typing in JavaScript is one of the strengths of the language and type checking is overrated. TypeScript adds convenience to a too-expensive price. And this is not the price for which I am willing to pay.

Douglas Crockford - creator of JSLint. Original

For Visual Studio users, TypeScript is a pretty good development tool, and besides, it fits perfectly with the ES6 standard. I could tell more about this language, but I see no reason to compare it with Dart.

Brendan Ike is the founder of JavaScript. Original

I am a huge fan of CoffeeScript, although this is another language with a separate syntax. What I like about TypeScript is that static typing allows for a compilation process with warnings, clever code refactoring. In addition to this, you get easy code navigation. In the current version of coffeScript you will not get such features.

Scott Hanselman - Microsoft evangelist. Original


pros


Quite a lot has been written about the advantages of TypeScript. Therefore we will try to note its advantages:



Minuses


It seems to me that in most cases TypeScript is praised. So I wanted to write my post about what is wrong with Typescript. And as it turned out, it was not easy to find cons.



Little about static typing


In the life of every developer there is a time when he writes code for his own pleasure. Whether it is a home project, or work in a team on a project that just started writing from scratch. This is one of the great moments when you don’t have to think a lot about conflicts in code with colleagues and look for errors. But the project grows, acquires new functionality and bugs, including types, if you write your code in a dynamically typed language, which is JavaScript.

What could be the solution in this case? Write tests!

But agree, why check what the machine can do well at the compilation stage? Static-typed languages ​​save us from over-writing additional tests that are associated with errors in the types. Therefore, TypeScript has a big advantage over JavaScript if we don’t ignore types.

Consider a simple example.


We have a function that can add two numbers:

function sum(a, b) { return a + b; } 


Your customer proposed to implement a web form in which the user could enter the folding numbers:

 var result = sum( document.getElementById("input1").value, document.getElementById("input2").value, ) document.getElementById("result").innerText = result; 


We write two numbers, 2 and 3 in our input fields and check the operation of our function:

 23 


The result was quite unexpected. The thing is that the value field of the html input tag returns the result of type “string” and JavaScript merges two strings “1” and “2” instead of adding these numbers.

An example, of course, is quite simple, but in real life errors can be more complicated, and they can be quite difficult to notice at the design stage.

The solution of this problem is quite easily solved using TypeScript:

 function sum(a: number, b: number): number { return a + b; } sum(2, 3); // Ok sum(2, "3"); // Error! 


Already at compile time we were able to detect the error. It is better to spend forces on development, but not on search of an error and writing of additional tests.

Conclusion


When I thought about the pros and cons of TypeScript development, it turned out to be hard to isolate the minuses. TypeScript more justifies itself in large projects. This is due to the fact that the development on it takes more time than on JavaScript, due to the fact that in addition to the methods and classes it is necessary to describe their declarations. But nevertheless, as long as there is no static typing in JavaScript, TypeScript is an excellent alternative.

PS The purpose of my article was to understand the advantages and disadvantages of TypeScript and most likely I missed something. Therefore, I will be glad to any comments on the merits.

Source: https://habr.com/ru/post/272055/


All Articles