Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61806a3

Browse files
committed
TS2.1中增强了对any的类型检查
1 parent 30c828f commit 61806a3

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

basics/type-inference.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,20 @@ myFavoriteNumber = 7;
2424

2525
TypeScript 会在没有明确的指定类型的时候推测出一个类型,这就是类型推论。
2626

27-
**如果定义的时候没有赋值,不管之后有没有赋值,都会被推断成 `any` 类型**
27+
**在TypeScript 2.1 之前,如果定义的时候没有赋值,不管之后有没有赋值,都会被推断成 `any` 类型而完全不被类型检查**
2828

2929
```ts
3030
let myFavoriteNumber;
3131
myFavoriteNumber = 'seven';
3232
myFavoriteNumber = 7;
3333
```
3434

35+
**TypeScript 2.1 中,编译器会考虑对 myFavoriteNumber 的最后一次赋值来检查类型**[](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html#improved-any-inference)
36+
3537
## 参考
3638

3739
- [Type Inference](http://www.typescriptlang.org/docs/handbook/type-inference.html)[中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Type%20Inference.html)
40+
- [TypeScript 2.1 Release Note](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html)
3841

3942
---
4043

0 commit comments

Comments
 (0)