2017-01-09 15:51:21 +08:00
|
|
|
|
# 类型推论
|
|
|
|
|
|
|
2017-01-14 16:21:21 +08:00
|
|
|
|
如果没有明确的指定类型,那么 TypeScript 会依照类型推论(Type Inference)的规则推断出一个类型。
|
2017-01-09 15:51:21 +08:00
|
|
|
|
|
|
|
|
|
|
## 什么是类型推论
|
|
|
|
|
|
|
|
|
|
|
|
以下代码虽然没有指定类型,但是会在编译的时候报错:
|
|
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
|
let myFavoriteNumber = 'seven';
|
|
|
|
|
|
myFavoriteNumber = 7;
|
|
|
|
|
|
|
|
|
|
|
|
// index.ts(2,1): error TS2322: Type 'number' is not assignable to type 'string'.
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
事实上,它等价于:
|
|
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
|
let myFavoriteNumber: string = 'seven';
|
|
|
|
|
|
myFavoriteNumber = 7;
|
|
|
|
|
|
|
|
|
|
|
|
// index.ts(2,1): error TS2322: Type 'number' is not assignable to type 'string'.
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
TypeScript 会在没有明确的指定类型的时候推测出一个类型,这就是类型推论。
|
|
|
|
|
|
|
2017-11-01 11:28:58 +08:00
|
|
|
|
**如果定义的时候没有赋值,不管之后有没有赋值,都会被推断成 `any` 类型而完全不被类型检查**:
|
2017-01-09 15:51:21 +08:00
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
|
let myFavoriteNumber;
|
|
|
|
|
|
myFavoriteNumber = 'seven';
|
|
|
|
|
|
myFavoriteNumber = 7;
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2017-01-14 16:21:21 +08:00
|
|
|
|
## 参考
|
2017-01-09 15:51:21 +08:00
|
|
|
|
|
2017-01-17 16:22:48 +08:00
|
|
|
|
- [Type Inference](http://www.typescriptlang.org/docs/handbook/type-inference.html)([中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Type%20Inference.html))
|