这一章节将讨论TypeScript中的类型推导,即我们将讨论在何处以及如何来推导类型。
TypeScript中,有好几处没有明确的类型注解(explicit type annotation)的地方需要使用类型推导来提供类型信息。例如在下面代码中:
var x = 3;
x变量的类型被推断为number。当初始化变量与成员,设置参数缺省值、确定函数返回类型时会做类型推断。
在多数情况下,类型推断是显而易见的。在下面章节中,我们将看看类型推导时的一些细节。
When a type inference is made from several expressions, the types of those expressions are used to calculate a "best common type". For example,
var x = [0, 1, null];
To infer the type of x in the example above, we must consider the type of each array element. Here we are given two choices for the type of the array: number and null. The best common type algorithm considers each candidate type, and picks the type that is compatible with all the other candidates.
Because the best common type has to be chosen from the provided candidate types, there are some cases where types share a common structure, but no one type is the super type of all candidate types. For example:
var zoo = [new Rhino(), new Elephant(), new Snake()];
Ideally, we may want zoo to be inferred as an Animal[], but because there is no object that is strictly of type Animal in the array, we make no inference about the array element type. To correct this, instead explicitly provide the type when no one type is a super type of all other candidates:
var zoo: Animal[] = [new Rhino(), new Elephant(), new Snake()];
When no best common type is found, the resulting inference is the empty object type, {}. Because this type has no members, attempting to use any properties of it will cause an error. This result allows you to still use the object in a type-agnostic manner, while providing type safety in cases where the type of the object can‘t be implicitly determined.
Type inference also works in "the other direction" in some cases in TypeScript. This is known as "contextual typing". Contextual typing occurs when the type of an expression is implied by its location. For example:
window.onmousedown = function(mouseEvent) {
console.log(mouseEvent.buton); //<- Error
};
For the code above to give the type error, the TypeScript type checker used the type of the Window.onmousedown function to infer the type of the function expression on the right hand side of the assignment. When it did so, it was able to infer the type of the mouseEvent parameter. If this function expression were not in a contextually typed position, the mouseEventparameter would have type any, and no error would have been issued.
If the contextually typed expression contains explicit type information, the contextual type is ignored. Had we written the above example:
window.onmousedown = function(mouseEvent: any) {
console.log(mouseEvent.buton); //<- Now, no error is given
};
The function expression with an explicit type annotation on the parameter will override the contextual type. Once it does so, no error is given as no contextual type applies.
Contextual typing applies in many cases. Common cases include arguments to function calls, right hand sides of assignments, type assertions, members of object and array literals, and return statements. The contextual type also acts as a candidate type in best common type. For example:
function createZoo(): Animal[] {
return [new Rhino(), new Elephant(), new Snake()];
}
In this example, best common type has a set of four candidates: Animal, Rhino, Elephant, and Snake. Of these, Animal can be chosen by the best common type algorithm.
[1] http://www.typescriptlang.org/Handbook#type-inference
[2] TypeScript系列1-简介及版本新特性, http://my.oschina.net/1pei/blog/493012
[3] TypeScript系列2-手册-基础类型, http://my.oschina.net/1pei/blog/493181
[4] TypeScript系列3-手册-接口, http://my.oschina.net/1pei/blog/493388
[5] TypeScript系列4-手册-类, http://my.oschina.net/1pei/blog/493539
[6] TypeScript系列5-手册-模块, http://my.oschina.net/1pei/blog/495948
[7] TypeScript系列6-手册-函数, http://my.oschina.net/1pei/blog/501273
[8] TypeScript手册翻译系列7-泛型, http://my.oschina.net/1pei/blog/513483
[9] TypeScript手册翻译系列8-常见错误与mixins, http://my.oschina.net/1pei/blog/513499
[10] TypeScript手册翻译系列9-声明合并, http://my.oschina.net/1pei/blog/513649
原文:http://my.oschina.net/1pei/blog/513652