From d90000bc96a32b628cbc21f705926820fafaa381 Mon Sep 17 00:00:00 2001 From: zhongsp Date: Fri, 28 Oct 2016 10:00:24 +0800 Subject: [PATCH] breaking changes: TypeScript 1.4. closed #131. --- README.md | 1 + SUMMARY.md | 3 +- doc/breaking-changes/TypeScript 1.4.md | 118 +++++++++++++++++++++++ doc/breaking-changes/breaking-changes.md | 1 + preface.md | 1 + 5 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 doc/breaking-changes/TypeScript 1.4.md diff --git a/README.md b/README.md index 52791d1..ba075d3 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ TypeScript是JavaScript的超集并且能够编译输出为纯粹的JavaScript. * [TypeScript 1.7](./doc/breaking-changes/TypeScript 1.7.md) * [TypeScript 1.6](./doc/breaking-changes/TypeScript 1.6.md) * [TypeScript 1.5](./doc/breaking-changes/TypeScript 1.5.md) + * [TypeScript 1.4](./doc/breaking-changes/TypeScript 1.4.md) **TypeScript Handbook** diff --git a/SUMMARY.md b/SUMMARY.md index 3a40734..7fa33d2 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -66,4 +66,5 @@ * [TypeScript 1.8](./doc/breaking-changes/TypeScript 1.8.md) * [TypeScript 1.7](./doc/breaking-changes/TypeScript 1.7.md) * [TypeScript 1.6](./doc/breaking-changes/TypeScript 1.6.md) - * [TypeScript 1.5](./doc/breaking-changes/TypeScript 1.5.md) \ No newline at end of file + * [TypeScript 1.5](./doc/breaking-changes/TypeScript 1.5.md) + * [TypeScript 1.4](./doc/breaking-changes/TypeScript 1.4.md) \ No newline at end of file diff --git a/doc/breaking-changes/TypeScript 1.4.md b/doc/breaking-changes/TypeScript 1.4.md new file mode 100644 index 0000000..083b988 --- /dev/null +++ b/doc/breaking-changes/TypeScript 1.4.md @@ -0,0 +1,118 @@ +# TypeScript 1.4 + +完整的破坏性改动列表请到这里查看:[breaking change issues](https://github.com/Microsoft/TypeScript/issues?q=is%3Aissue+milestone%3A%22TypeScript+1.4%22+label%3A%22breaking+change%22)。 + +阅读[issue #868](https://github.com/Microsoft/TypeScript/issues/868)以了解更多关于联合类型的破坏性改动。 + +#### 多个最佳通用类型候选 + +当有多个最佳通用类型可用时,现在编译器会做出选择(依据编译器的具体实现)而不是直接使用第一个。 + +```ts +var a: { x: number; y?: number }; +var b: { x: number; z?: number }; + +// 之前 { x: number; z?: number; }[] +// 现在 { x: number; y?: number; }[] +var bs = [b, a]; +``` + +这会在多种情况下发生。具有一组共享的必需属性和一组其它互斥的(可选或其它)属性,空类型,兼容的签名类型(包括泛型和非泛型签名,当类型参数上应用了```any```时)。 + +**推荐** +使用类型注解指定你要使用的类型。 +```ts +var bs: { x: number; y?: number; z?: number }[] = [b, a]; +``` + +#### 泛型接口 + +当在多个T类型的参数上使用了不同的类型时会得到一个错误,就算是添加约束也不行: + +```ts +declare function foo(x: T, y:T): T; +var r = foo(1, ""); // r used to be {}, now this is an error +``` +添加约束: + +```ts +interface Animal { x } +interface Giraffe extends Animal { y } +interface Elephant extends Animal { z } +function f(x: T, y: T): T { return undefined; } +var g: Giraffe; +var e: Elephant; +f(g, e); +``` + +在这里查看[详细解释](https://github.com/Microsoft/TypeScript/pull/824#discussion_r18665727)。 + +**推荐** +如果这种不匹配的行为是故意为之,那么明确指定类型参数: + +```ts +var r = foo<{}>(1, ""); // Emulates 1.0 behavior +var r = foo(1, ""); // Most useful +var r = foo(1, ""); // Easiest +f(g, e); +``` + +*或*重写函数定义指明就算不匹配也没问题: + +```ts +declare function foo(x: T, y:U): T|U; +function f(x: T, y: U): T|U { return undefined; } +``` + +#### 泛型剩余参数 + +不能再使用混杂的参数类型: + +```ts +function makeArray(...items: T[]): T[] { return items; } +var r = makeArray(1, ""); // used to return {}[], now an error +``` + +`new Array(...)`也一样 + +**推荐** +声明向后兼容的签名,如果1.0的行为是你想要的: + +```ts +function makeArray(...items: T[]): T[]; +function makeArray(...items: {}[]): {}[]; +function makeArray(...items: T[]): T[] { return items; } +``` + +#### 带类型参数接口的重载解析 + +```ts +var f10: (x: T, b: () => (a: T) => void, y: T) => T; +var r9 = f10('', () => (a => a.foo), 1); // r9 was any, now this is an error +``` + +**推荐** +手动指定一个类型参数 + +```ts +var r9 = f10('', () => (a => a.foo), 1); +``` + +#### 类声明与类型表达式以严格模式解析 + +ECMAScript 2015语言规范(ECMA-262 6th Edition)指明*ClassDeclaration*和*ClassExpression*使用严格模式。 +因此,在解析类声明或类表达式时将使用额外的限制。 + +例如: + +```ts +class implements {} // Invalid: implements is a reserved word in strict mode +class C { + foo(arguments: any) { // Invalid: "arguments" is not allow as a function argument + var eval = 10; // Invalid: "eval" is not allowed as the left-hand-side expression + arguments = []; // Invalid: arguments object is immutable + } +} +``` + +关于严格模式限制的完整列表,请阅读 Annex C - The Strict Mode of ECMAScript of ECMA-262 6th Edition。 \ No newline at end of file diff --git a/doc/breaking-changes/breaking-changes.md b/doc/breaking-changes/breaking-changes.md index 5ecf272..0047c61 100644 --- a/doc/breaking-changes/breaking-changes.md +++ b/doc/breaking-changes/breaking-changes.md @@ -5,3 +5,4 @@ * [TypeScript 1.7](./TypeScript 1.7.md) * [TypeScript 1.6](./TypeScript 1.6.md) * [TypeScript 1.5](./TypeScript 1.5.md) +* [TypeScript 1.4](./TypeScript 1.4.md) diff --git a/preface.md b/preface.md index 93e4b31..209e2a8 100644 --- a/preface.md +++ b/preface.md @@ -90,6 +90,7 @@ TypeScript目前还在积极的开发完善之中,不断地会有新的特性 * [TypeScript 1.7](./doc/breaking-changes/TypeScript 1.7.html) * [TypeScript 1.6](./doc/breaking-changes/TypeScript 1.6.html) * [TypeScript 1.5](./doc/breaking-changes/TypeScript 1.5.html) + * [TypeScript 1.4](./doc/breaking-changes/TypeScript 1.4.html) ## 主要修改 (Latest 10 updates)