Why you should use TypeScript

    If you aren’t in the know: JavaScript won . Today it is the most cross-platform language available for any device. On it you can create web applications (client and server), including those with offline mode, desktop applications (for Windows 8), applications for smartphones and tablets (PhoneGap), extensions for Microsoft Office, SharePoint and Dynamics. JavaScript code works in DBMSs such as MongoDB and even Hadoop in Windows Azure (BigData however).

    Doom and the Linux emulator are already written in Javascript. In fact, solving any task other than writing an OS kernel module, you will come across JavaScript. If you do not already know JavaScript, then you should urgently start learning it.

    JavaScript has a lot of flaws

    Many developers experience this kind of reaction to JavaScript:


    (image taken from here )

    Most people are scared by the lack of intellisense when typing code and manipulating HTML \ CSS. But these are not the biggest problems.

    The biggest problem with JavaScript is that it wasn’t invented by Microsoft.

    The main disadvantages of JavaScript:

    • Dynamic typing that causes a lot of regression errors.
    • Lack of modularity. There are no modules or classes, the prototype OOP breaks the brain for those who write in C ++ \ Java \ C #.
    • Unobvious behavior in many places.

    In order not to write JavaScript, Java -> JavaScript, C # -> JavaScript, LLVM -> JavaScript compilers were created. But all this leads to the fact that significant restrictions are imposed on the program in the source language, and also the existing libraries for JavaScript are not used.

    TypeScript fixes some of the flaws

    Microsoft, looking at this picture, decided that we need a language that on the one hand fixes problems, on the other hand it is as close as possible to JavaScript in order to use existing developments.

    So the TypeScript language appeared ( link to offsite ). TypeScript is a superset of JavaScript. That is, any valid JavaScript code is also valid TypeScript code.

    TypeScript uses static typing, that is, all types are checked at compilation time. By the way, the TypeScript compiler itself is written in TypeScript and is open source ( link to the repository ).

    TypeScript adds the ability to declare modules, classes, and interfaces. This allows you to scale the development of complex JavaScript applications.

    The output is plain JavaScript, which does not require additional libraries or special components.

    TypeScript in action

    When typing code in VisualStudio, a rich hint is available:


    classes, annotations and type inference are supported, interactive debugging works.

    You can easily use external libraries in TypeScript, for example jquery:


    When types do not match, the compiler swears:


    TypeScript compiles when building the project, checking for many errors without starting it.

    Well, the most important feature, from the look of which I almost burst into tears:


    Those who are still not completely inspired can look at the raytracer on TypeScript , the result of the work .

    How to start using TypeScript

    If you use VisualStudio, then you need to put two extensions:

    Then you will get this editing mode:


    On the left is TypeScript code, on the right is the result of compilation in JavaScript. In this way, using TypeScript will help you better understand and learn JavaScript.

    Those who do not use visual studio are a little less fortunate. Today plugins are available for common text editors , as well as command line tools.

    Next, you should definitely read the tutorials from the real dad , sometimes it's worth looking into the specification .

    Many type definitions for popular libraries can be found in the DefinitelyTyped project . Judging by the nickname of the author of the project, this is our compatriot.


    This was an introductory article. Next time more examples and real application of TypeScript.

    Also popular now: