TypeScript 1.8

完整的破坏性改动列表请到这里查看:breaking change issues

现在生成模块代码时会带有"use strict";

在ES6模式下模块总是在严格模式下解析,对于生成目标为非ES6的却不是这样。从TypeScript 1.8开始,生成的模块将总为严格模式。这应该不会对现有的大部分代码产生影响,因为TypeScript把大多数因为严格模式而产生的错误当做编译时错误,但还是有一些在运行时才发生错误的TypeScript代码,比如赋值给NaN,现在将会直接报错。你可以参考MDN Article学习关于严格模式与非严格模式的区别。

若想禁用这个行为,在命令行里传--noImplicitUseStrict选项或在tsconfig.json文件里指定。

从模块里导出非局部名称

依据ES6/ES2015规范,从模块里导出非局部名称将会报错。

例子

export { Promise }; // Error

推荐

在导出之前,使用局部变量声明捕获那个全局名称。

const localPromise = Promise;
export { localPromise as Promise };

默认启用代码可达性(Reachability)检查

TypeScript 1.8里,我们添加了一些可达性检查来阻止一些种类的错误。特别是:

  1. 检查代码的可达性(默认启用,可以通过allowUnreachableCode编译器选项禁用)

       function test1() {
           return 1;
           return 2; // error here
       }
    
       function test2(x) {
           if (x) {
               return 1;
           }
           else {
               throw new Error("NYI")
           }
           var y = 1; // error here
       }
    
  2. 检查标签是否被使用(默认启用,可以通过allowUnusedLabels编译器选项禁用)

    l: // error will be reported - label `l` is unused
    while (true) {
    }
    
    (x) => { x:x } // error will be reported - label `x` is unused
    
  3. 检查是否函数里所有带有返回值类型注解的代码路径都返回了值(默认启用,可以通过noImplicitReturns编译器选项禁用)

    // error will be reported since function does not return anything explicitly when `x` is falsy.
    function test(x): number {
       if (x) return 10;
    }
    
  4. 检查控制流是否能进到switch语句的case里(默认禁用,可以通过noFallthroughCasesInSwitch编译器选项启用)。注意没有语句的case不会被检查。

    switch(x) {
       // OK
       case 1:
       case 2:
           return 1;
    }
    switch(x) {
       case 1:
           if (y) return 1;
       case 2:
           return 2;
    }
    

如果你看到了这些错误,但是你认为这时的代码是合理的话,你可以通过编译选项来阻止报错。

--module不允许与--outFile一起出现,除非 --module被指定为amdsystem

之前使用模块指定这两个的时候,会生成空的out文件且不会报错。

标准库里的DOM API变动

  • ImageData.data现在的类型为Uint8ClampedArray而不是number[]。查看#949
  • HTMLSelectElement .options现在的类型为HTMLCollection而不是HTMLSelectElement。查看#1558
  • HTMLTableElement.createCaptionHTMLTableElement.createTBodyHTMLTableElement.createTFootHTMLTableElement.createTHeadHTMLTableElement.insertRowHTMLTableSectionElement.insertRowHTMLTableElement.insertRow现在返回HTMLTableRowElement而不是HTMLElement。查看#3583
  • HTMLTableRowElement.insertCell现在返回HTMLTableCellElement而不是HTMLElement查看#3583
  • IDBObjectStore.createIndexIDBDatabase.createIndex第二个参数类型为IDBObjectStoreParameters而不是any。查看#5932
  • DataTransferItemList.Item返回值类型变为DataTransferItem而不是File。查看#6106
  • Window.open返回值类型变为Window而不是any。查看#6418
  • WeakMap.clear被移除。查看#6500

在super-call之前不允许使用this

ES6不允许在构造函数声明里访问this

比如:

class B {
    constructor(that?: any) {}
}

class C extends B {
    constructor() {
        super(this);  // error;
    }
}

class D extends B {
    private _prop1: number;
    constructor() {
        this._prop1 = 10;  // error
        super();
    }
}

书籍推荐