Skip to content

Commit d90000b

Browse files
committed
breaking changes: TypeScript 1.4. closed zhongsp#131.
1 parent 1c8458e commit d90000b

5 files changed

Lines changed: 123 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ TypeScript是JavaScript的超集并且能够编译输出为纯粹的JavaScript.
8080
* [TypeScript 1.7](./doc/breaking-changes/TypeScript 1.7.md)
8181
* [TypeScript 1.6](./doc/breaking-changes/TypeScript 1.6.md)
8282
* [TypeScript 1.5](./doc/breaking-changes/TypeScript 1.5.md)
83+
* [TypeScript 1.4](./doc/breaking-changes/TypeScript 1.4.md)
8384

8485

8586
**TypeScript Handbook**

SUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,5 @@
6666
* [TypeScript 1.8](./doc/breaking-changes/TypeScript 1.8.md)
6767
* [TypeScript 1.7](./doc/breaking-changes/TypeScript 1.7.md)
6868
* [TypeScript 1.6](./doc/breaking-changes/TypeScript 1.6.md)
69-
* [TypeScript 1.5](./doc/breaking-changes/TypeScript 1.5.md)
69+
* [TypeScript 1.5](./doc/breaking-changes/TypeScript 1.5.md)
70+
* [TypeScript 1.4](./doc/breaking-changes/TypeScript 1.4.md)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# TypeScript 1.4
2+
3+
完整的破坏性改动列表请到这里查看:[breaking change issues](https://github.com/Microsoft/TypeScript/issues?q=is%3Aissue+milestone%3A%22TypeScript+1.4%22+label%3A%22breaking+change%22)
4+
5+
阅读[issue #868](https://github.com/Microsoft/TypeScript/issues/868)以了解更多关于联合类型的破坏性改动。
6+
7+
#### 多个最佳通用类型候选
8+
9+
当有多个最佳通用类型可用时,现在编译器会做出选择(依据编译器的具体实现)而不是直接使用第一个。
10+
11+
```ts
12+
var a: { x: number; y?: number };
13+
var b: { x: number; z?: number };
14+
15+
// 之前 { x: number; z?: number; }[]
16+
// 现在 { x: number; y?: number; }[]
17+
var bs = [b, a];
18+
```
19+
20+
这会在多种情况下发生。具有一组共享的必需属性和一组其它互斥的(可选或其它)属性,空类型,兼容的签名类型(包括泛型和非泛型签名,当类型参数上应用了```any```时)。
21+
22+
**推荐**
23+
使用类型注解指定你要使用的类型。
24+
```ts
25+
var bs: { x: number; y?: number; z?: number }[] = [b, a];
26+
```
27+
28+
#### 泛型接口
29+
30+
当在多个T类型的参数上使用了不同的类型时会得到一个错误,就算是添加约束也不行:
31+
32+
```ts
33+
declare function foo<T>(x: T, y:T): T;
34+
var r = foo(1, ""); // r used to be {}, now this is an error
35+
```
36+
添加约束:
37+
38+
```ts
39+
interface Animal { x }
40+
interface Giraffe extends Animal { y }
41+
interface Elephant extends Animal { z }
42+
function f<T extends Animal>(x: T, y: T): T { return undefined; }
43+
var g: Giraffe;
44+
var e: Elephant;
45+
f(g, e);
46+
```
47+
48+
在这里查看[详细解释](https://github.com/Microsoft/TypeScript/pull/824#discussion_r18665727)
49+
50+
**推荐**
51+
如果这种不匹配的行为是故意为之,那么明确指定类型参数:
52+
53+
```ts
54+
var r = foo<{}>(1, ""); // Emulates 1.0 behavior
55+
var r = foo<string|number>(1, ""); // Most useful
56+
var r = foo<any>(1, ""); // Easiest
57+
f<Animal>(g, e);
58+
```
59+
60+
**重写函数定义指明就算不匹配也没问题:
61+
62+
```ts
63+
declare function foo<T,U>(x: T, y:U): T|U;
64+
function f<T extends Animal, U extends Animal>(x: T, y: U): T|U { return undefined; }
65+
```
66+
67+
#### 泛型剩余参数
68+
69+
不能再使用混杂的参数类型:
70+
71+
```ts
72+
function makeArray<T>(...items: T[]): T[] { return items; }
73+
var r = makeArray(1, ""); // used to return {}[], now an error
74+
```
75+
76+
`new Array(...)`也一样
77+
78+
**推荐**
79+
声明向后兼容的签名,如果1.0的行为是你想要的:
80+
81+
```ts
82+
function makeArray<T>(...items: T[]): T[];
83+
function makeArray(...items: {}[]): {}[];
84+
function makeArray<T>(...items: T[]): T[] { return items; }
85+
```
86+
87+
#### 带类型参数接口的重载解析
88+
89+
```ts
90+
var f10: <T>(x: T, b: () => (a: T) => void, y: T) => T;
91+
var r9 = f10('', () => (a => a.foo), 1); // r9 was any, now this is an error
92+
```
93+
94+
**推荐**
95+
手动指定一个类型参数
96+
97+
```ts
98+
var r9 = f10<any>('', () => (a => a.foo), 1);
99+
```
100+
101+
#### 类声明与类型表达式以严格模式解析
102+
103+
ECMAScript 2015语言规范(ECMA-262 6<sup>th</sup> Edition)指明*ClassDeclaration**ClassExpression*使用严格模式。
104+
因此,在解析类声明或类表达式时将使用额外的限制。
105+
106+
例如:
107+
108+
```ts
109+
class implements {} // Invalid: implements is a reserved word in strict mode
110+
class C {
111+
foo(arguments: any) { // Invalid: "arguments" is not allow as a function argument
112+
var eval = 10; // Invalid: "eval" is not allowed as the left-hand-side expression
113+
arguments = []; // Invalid: arguments object is immutable
114+
}
115+
}
116+
```
117+
118+
关于严格模式限制的完整列表,请阅读 Annex C - The Strict Mode of ECMAScript of ECMA-262 6<sup>th</sup> Edition。

doc/breaking-changes/breaking-changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
* [TypeScript 1.7](./TypeScript 1.7.md)
66
* [TypeScript 1.6](./TypeScript 1.6.md)
77
* [TypeScript 1.5](./TypeScript 1.5.md)
8+
* [TypeScript 1.4](./TypeScript 1.4.md)

preface.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ TypeScript目前还在积极的开发完善之中,不断地会有新的特性
9090
* [TypeScript 1.7](./doc/breaking-changes/TypeScript 1.7.html)
9191
* [TypeScript 1.6](./doc/breaking-changes/TypeScript 1.6.html)
9292
* [TypeScript 1.5](./doc/breaking-changes/TypeScript 1.5.html)
93+
* [TypeScript 1.4](./doc/breaking-changes/TypeScript 1.4.html)
9394

9495

9596
## 主要修改 (Latest 10 updates)

0 commit comments

Comments
 (0)