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.

  1. ts
    function longest<T extends { length: number }>(a: T, b: T) {
  2. if (a.length >= b.length) {
  3. return a;
  4. } else {
  5. return b;
  6. }
  7. }
  8. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. // longerArray is of type 'number[]'
  10. const longerArray = longest([1, 2], [1, 2, 3]);
  11. // longerString is of type 'string'
  12. const longerString = longest("alice", "bob");
  13. // Error! Numbers don't have a 'length' property
  14. const notOK = longest(10, 100);
  15. 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; }'.
  16. const hello = longest("alice", "bob");
  17. 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:

  1. ts
    module MyControllers {
  2. interface ZooScope extends ng.IScope {
  3. animals: Animal[];
  4. }
  5. export class ZooController {
  6. // Used to be an error (cannot expose ZooScope), but now is only
  7. // an error when trying to generate .d.ts files
  8. constructor(public $scope: ZooScope) {}
  9. /* more code */
  10. }
  11. }