This is a test example, knowingly shipped to prod while v2 is in beta - it’s got a really long comment and a twoslash error.
tsfunction
longest <T extends {length : number }>(a :T ,b :T ) { if (a .length >=b .length ) { returna ; } else { returnb ; } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // longerArray is of type 'number[]' constlongerArray =longest ([1, 2], [1, 2, 3]); // longerString is of type 'string' constlongerString =longest ("alice", "bob"); // Error! Numbers don't have a 'length' property constnotOK =longest (10 , 100); Argument of type '10' is not assignable to parameter of type '{ length: number; }'.2345Argument of type '10' is not assignable to parameter of type '{ length: number; }'. consthello =longest ("alice", "bob");console .log (hello );
Performance Improvements
The 1.1 compiler is typically around 4x faster than any previous release. See this blog post for some impressive charts.
Better Module Visibility Rules
TypeScript now only strictly enforces the visibility of types in modules if the --declaration
flag is provided. This is very useful for Angular scenarios, for example:
tsmodule MyControllers { interface ZooScope extends ng.IScope { animals: Animal[]; } export class ZooController { // Used to be an error (cannot expose ZooScope), but now is only // an error when trying to generate .d.ts files constructor(public $scope: ZooScope) {} /* more code */ } }