TypeScript 2.0

对函数或类表达式的捕获变量不进行类型细化(narrowing)

var x: number | string;

if (typeof x === "number") {
function inner(): number {
return x; // Error, type of x is not narrowed, c is number | string
}
var y: number = x; // OK, x is number
}

var x: number | string = "a";
if (typeof x === "string") {
setTimeout(() => console.log(x.charAt(0)), 0);
}
x = 5;

x.charAt()被调用的时候把x的类型当作string是错误的，事实上它确实不是string类型。

const x: number | string = "a";
if (typeof x === "string") {
setTimeout(() => console.log(x.charAt(0)), 0);
}

泛型参数会进行类型细化

function g(obj: T) {
var t: T;
if (obj instanceof RegExp) {
t = obj; // RegExp is not assignable to T
}
}

class C {
get x() { return 0; }
}

var c = new C();
c.x = 1; // Error Left-hand side is a readonly property

在严格模式下函数声明不允许出现在块(block)里

if( true ) {
function foo() {}
}

export = foo;

if( true ) {
const foo = function() {}
}

TemplateStringsArray现是是不可变的

ES2015模版字符串总是将它们的标签以不可变的类数组对象进行传递，这个对象带有一个raw属性（同样是不可变的）。 TypeScript把这个对象命名为TemplateStringsArray

function myTemplateTag(strs: string[]) {
// ...
}
{"ft_lang_label":"__label__zh","ft_lang_prob":0.5617002,"math_prob":0.9983244,"size":1519,"snap":"2019-43-2019-47","text_gpt3_token_len":746,"char_repetition_ratio":0.12739274,"word_repetition_ratio":0.12682927,"special_character_ratio":0.28044766,"punctuation_ratio":0.1790393,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9527954,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-10-16T10:30:59Z\",\"WARC-Record-ID\":\"<urn:uuid:de8648a2-a1f5-49d6-a924-e04ad573892d>\",\"Content-Length\":\"47685\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:67578fca-afef-40cb-9d5c-adb43c175849>\",\"WARC-Concurrent-To\":\"<urn:uuid:490bc9b7-bcb4-4d69-b27a-6ed8271b17b7>\",\"WARC-IP-Address\":\"120.24.76.145\",\"WARC-Target-URI\":\"http://caibaojian.com/typescript/doc/breaking-changes/TypeScript%202.0.html\",\"WARC-Payload-Digest\":\"sha1:A7JL7SWSLCAWPENYUS55FAN7YLZM45AY\",\"WARC-Block-Digest\":\"sha1:FZ45IU4KXE3CPJR4LHPZJ3CIFOD4NAIE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-43/CC-MAIN-2019-43_segments_1570986666959.47_warc_CC-MAIN-20191016090425-20191016113925-00020.warc.gz\"}"}