Dajbych.net


What TypeScript is good for

, 5 minutes to read

typescript logo

TypeScript is a JavaScript extension. At the end of the year, version 1.0 will be released, which will be integrated into Visual Studio 2013. While JavaScript was designed for applications containing hundreds of lines of code, TypeScript is designed for applications containing hundreds of thousands of lines of code. It offers type checking, classes, interface modules, IntelliSense, method renaming capabilities, Go To Definiton and Find All References. Its syntax is designed to be as similar as possible to that of ECMAScript 6.

Why doesn't JavaScript have type checking?

TypeScript therefore offers static type checking. Why learn TypeScript and not wait until it’s in JavaScript sometime in the future? Because the introduction of types was part of the ECMASCript 4 specification, but in the end everyone kept their hands off it, until a lightweight ECMAScript 6 specification was created, which does not contain types. Currently, type checking for JavaScript is not in sight.

The Chakra JavaScript runtime environment, which is in Internet Explorer, creates data types dynamically as code is executed to improve performance. From a distance, the result is somewhat strange. A typed language, which is necessary for a large amount of code, is translated into a language without type checking, in which types are difficult to find, because without this it is not possible to achieve high performance on current processors.

Static data types are a necessity for developing tools for developers, which can then offer support for code refactoring, reference search, and of course, IntelliSense.

For example, if we call REST services that return data in JSON format, we do not know their schema. The easiest way to parse them is to use a dynamic language that maps the language’s methods to data (method JSON.parse). For this scenario, purely dynamic language makes sense. However, when we work with this data, we need to have its structure in mind. It’s easy to make mistakes. TypeScript provides an interface that describes what properties an object has and what type they are, replacing the schema. This allows you to use IntelliSense for your own data.

Another example is working with a DOM, the structure of which is known in advance. This makes it possible to use IntelliSense, which speeds up work. The file that describes DOM data types and from which Visual Studio draws information for IntellSense to TypeScript has over 8,000 lines. No one can remember them all anymore. TypeScript, however, does.

Where is TypeScript most used?

The pilot application for testing TypeScript is the Xbox Music application, which runs on Windows 8 and Xbox One. It contains over half a million lines of typescript code. TypeScript is also used by the Bing Maps, Office 365, and Office Web Apps product teams. Other TypeScript users include Adobe.

What makes TypeScript different from similar languages?

There are similar languages that bring similar benefits. These include CoffeeScript, Script#, and Dart. While TypeScript is a JavaScript extension, CoffeeScript is a distinct language and is not typed. For example, you can't take your JavaScript files, rename them to CoffeeScript, and extend them. Dart offers a different environment than JavaScript, but at the cost of having to create it artificially in JavaScript. This significantly reduces performance. Script# is similar. TypeScript injects your code only when you use inheritance. Even so, it is only 6 lines of code. TypeScript is the only language that is an extension of JavaScript and offers static types.

The strictly typed C# language also has a dynamic data type. It does what dynamic languages, such as JavaScript, do, with the only difference being that it monitors typeing at runtime. JavaScript has its own system and TypeScript does not try to change it in any way. It only intentionally binds the programmer more and thus provides him with more support, so he does not make a mistake so easily. All type is lost at the time of translation. Therefore, it does not reduce performance.

IntelliSense for JavaScript as well

IntelliSense support for WinJS should be taken for granted. Is WinJS TypeScript? Yes, because JavaScript is a subset of TypeScript. The question is whether WinJS is written in TypeScript to support type checking. However, it doesn't matter, because type checking can also be provided for javascript code. However, a declaration file is needed for this. Currently, declaration files are available for most JavaScript libraries. You can find them on the DefinitelyTyped page.

IntelliSense requires a typed language. This example illustrates how data types flow through code.

var a = ["hello", "world"];
var n = a.map(s => s.length);
/*
proměnná a je typu array<string>
proměnná s je typu string a Visual Studio může nebídnout IntelliSence
proměnná n je typu array<number>
*/

What if I don't have Visual Studio?

TypeScript is open source and is also supported by development environments for Mac OS X or Linux. In addition to Visual Studio , it is also supported by JetBrains WebStorm and PhpStorm.

Asynchronous code in the future

Why doesn't TypeScript have async & await yet, along the lines of F# and C#? Although there is already a fork of TypeScript that supports this convenient asynchronous programming, Anders Hejlsberg decided to wait until ECMAScript 6. It will support iterators that allow asynchronous TypeScript code to be compiled into JavaScript much more efficiently. Without iterators, the code would be too different from the source code, and it would be very difficult to implement debugging support in Visual Studio.

Summary

So what does TypeScript offer?

Examples

I usually try to give code examples, but in this case, I decided to refer to the typescriptlang.org website, where all the virtues of TypeScript are explained in a concise and illustrative way.