Thoughts Aloud About TypeScript

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

    So, what exactly is TypeScript itself - probably it’s no 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 you to solve can be found on the official websiteor here on a hub , the benefit of articles is already written quite a lot. There is already an article of the similar TypeScript on the hub: general impressions , therefore, in order not to repeat myself, I decided to highlight the pros and cons of working with the language, based on my personal experience. It was not easy to recall and list the pros and cons of the language.


    Language popularity


    The TypeScript project was created by Microsoft. In fact, its creator is Anners Halesberg . From the very beginning, TypeScript has quickly gained popularity because of its flexibility and performance. A lot of projects that were written in JavaScript began to be ported to TypeScript. The growing interest in this language is also due to 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 written in this language, including such large ones as Angular 2.0 and vscode .

    Quotes of Authoritative People


    TypeScript is arguably one of the best front-end JavaScript languages. The code that it generates looks most attractive. And I think that he is able to take the load off the ECMAScript standard for implementing things like 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 too expensive. And this is not the price, for which I am willing to pay.

    Douglas Crockford - creator JSLint. Original

    For Visual Studio users, TypeScript is a pretty good development tool, and it is also very ES6 compliant. I could talk 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 it is another language with its own syntax. What I like about TypeScript is that static typing allows us to provide a compilation process with warnings and smart 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 is a Microsoft evangelist. Original


    pros


    Quite a lot has been written about the pros of TypeScript. Therefore, we will try to point out the advantages of it:

    • Support for many popular IDEs:

    • TypeScript is a strongly typed ( optional! ) And compiled in JavaScript language. It’s easier for programmers to learn Java and C #.
    • TypeScript implements many OOP concepts, such as inheritance, polymorphism, encapsulation, and access modifiers. It has classes, interfaces, and (even!) Abstract classes.
    • The potential of the language makes it faster and easier to write complex integrated solutions that are easier to develop and test in the future than with standard JavaScript.
    • TypeScript is a superset of JavaScript, so any JavaScript code will be executed in TypeScript as well. This, in my opinion, is its main advantage over competitors - for example, Dart from Google, which is a radically redesigned language from Javascript. There is an article on translating a Javascript project into TypeScript


    Minuses


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

    • In the process of development, we are dealing with * .ts, * .d.ts, * .map, * .js files. Too many additional files, which can be inconvenient if your project is small.
    • Not all browsers support TypeScript debugging in the console without unnecessary settings.
    • Many non-trivial classes. To write code based on classes, you have to keep in mind which property is where. For example, instead of one Event class, there are still such as MouseEvent, TouchEvent, KeyboardEvent and others ...
    • Implicit static typing. You can always describe a type as any, which in fact will disable casting to a specific type of this variable.
    • This is a transporter, which implies that we must always have tsc on hand
    • d.ts declarations are supported by the DefinitelyTyped community and often do not match the current version of the library. Or they don’t take into account complex variants (generic functions, return values ​​of several types)


    A bit about static typing


    In the life of every developer, there is a time when he writes code for his pleasure. Whether it's a home project, or teamwork on a project that has just begun to write from scratch. This is one of the wonderful moments when you don’t have to think a lot about conflicts in the code with colleagues and look for errors. But the project is growing, it is overgrown with new functionality and bugs related including with 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 you must admit, why check what the machine can handle well at the compilation stage? Languages ​​with static typing save us from unnecessarily writing additional tests that are associated with type errors. Therefore TypeScript has a big advantage over JavaScript if we do not ignore types.

    Let's look at a simple example.


    We have a function that can add two numbers:

    functionsum(a, b) {
      return a + b;
    }
    


    Your customer proposed to implement a web form in which the user could enter the added 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 a result of type “string” and JavaScript glues two strings “1” and “2” instead of adding these numbers.

    The example, of course, is quite simple, but in real life errors can be more complicated, and it can be quite difficult to notice them at the development stage.

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

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


    Already at the compilation stage, we were able to detect an error. It’s better to spend energy on developing, rather than searching for errors and writing additional tests.

    Conclusion


    When I thought about the pros and cons of TypeScript development, highlighting the cons was not easy. TypeScript is more than justified in large projects. This is due to the fact that the development on it takes longer than on JavaScript, due to the fact that in addition to methods and classes it is necessary to describe their declarations. Nevertheless, as long as there is no static typing in JavaScript, TypeScript is a great 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.

    Only registered users can participate in the survey. Please come in.

    What programming language do you use on the client?


    Also popular now: