⬆️ ⬇️

Announcement of new features Typescript 1.4

With the release of Typescript 1.3, we focused on improving the type system and adding ECMAScript 6 functionality to TypeScript. Let's look at some new features that you can use in the new version.



All the things described in the article are already implemented in the master branch of our repository on Github - you can download it and try them now.





')

New features allow you to work more accurately and easily with variables that are of a different type during execution. They reduce the number of places where you need to explicitly specify the type, check it, or use the type any . Authors of typing files ( .d.ts ) can also use these features to describe external libraries. Those who follow the development of the compiler may have noticed that we ourselves also use them.



Union types



General information



Join types are a powerful way to express a value that can be one of several types. Suppose you can have an API for executing a program that accepts command line arguments in the form of string or string[] . Now you can write this:



 interface RunOptions { program: string; commandline: string[]|string; } 


Assigning values ​​of this type works intuitively - any value that could be assigned to any member of the enumeration will work:



 var opts: RunOptions = /* ... */; opts.commandline = '-hello world'; // OK opts.commandline = ['-hello', 'world']; // OK opts.commandline = [42]; // :  number    string  string[] 


When accessing a variable, you can directly use the properties that are common to all types in the union:



 if(opts.commandline.length === 0) { //  string  string[]   length console.log("!"); } 


With the help of type terminators, it is easy and simple to work with a union-type variable:



 function formatCommandline(c: string[]|string) { if(typeof c === 'string') { return c.trim(); } else { return c.join(' '); } } 


Type limiters



A common practice in Javascript is to use typeof or instanceof operators to determine the type of a value at runtime. Typescript now understands these constructs and uses them when inferring a type, if they are used in the condition block:



 var x: any = /* ... */; if(typeof x === 'string') { console.log(x.subtr(1)); // :   'string'   'subtr' } //      'x'    'any' x.unknown(); // OK 


This is how instanceof can be used with classes and combined types:



 class Dog { woof() { } } class Cat { meow() { } } var pet: Dog|Cat = /* ... */; if(pet instanceof Dog) { pet.woof(); // OK } else { pet.woof(); // Error } 


Stricter generic types



We also decided to make verification of generalized calls more stringent in some cases. For example, before such a code, oddly enough, compiled without errors:



 function equal<T>(lhs: T, rhs: T): boolean { return lhs === rhs; } // :   //  :  -     'number'  'string' var e = equal(42, 'hello'); 


Improved type inference



Generalized types allow you to improve the quality of checks when using arrays and in other places where it is possible to use several different types in one collection:



 var x = [1, 'world']; // x: Array<string|number> x[0] = 'hello'; // OK x[0] = false; //  -  'boolean'    'number',  'string' 


Type Aliases



You can declare an alias for a type using the type keyword:



 type PrimitiveArray = Array<string|number|boolean>; type MyNumber = number; type NgScope = ng.IScope; type Callback = () => void; 


A type alias is its full synonym; they are completely interchangeable when used.



In the next article I will talk about the features of ECMAScript 6, which we add to Typescript. To learn more and try it yourself, download the master branch from the Typescript repository on Github , try it and share with us.



Translator's Note





In Typescript 1.3, it became possible to use tuples from arrays. However, automatic type inference does not occur:



 // : [number, string] //  : Array<string|number> var x = [1, 'world']; 


Why did they do that? Automatic derivation of tuple types breaks many usage scenarios, for example:



 var x = [dog, cat, animal]; //   'x'  'Animal[]' x[0] = new Frog(); 


If the type of the variable x were displayed as a tuple [Dog, Cat, Animal] , then the assignment in the second line would cause an error. The authors considered it more correct to require explicit indication of tuples, and this sounds rather logical.



In addition, tuples are compatible with transfer types unilaterally:

 var x : [number, string] = [1, "test"]; var y : Array<number|string> = x; //    x = y; // :       


Rather, support for these features would fall into Resharper!



UPD: Is it time to create a separate hub for Typescript?

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



All Articles