首頁 > web前端 > js教程 > 主體

TypeScript Type Innference(類型判斷)_javascript技巧

WBOY
發布: 2016-05-16 15:11:17
原創
1753 人瀏覽過

TypeScript is a superset of JavaScript developed by Microsoft. TypeScript is compatible with JavaScript and can load JavaScript code and then run it. The improvements of TypeScript compared with JavaScript include: adding comments to let the compiler understand the supported objects and functions. The compiler will remove the comments without increasing overhead; adding a complete class structure to update it is a traditional orientation. object language.

Why is there TypeScript?

JavaScript is just a scripting language and is not designed to develop large-scale web applications. JavaScript does not provide the concepts of classes and modules, and TypeScript extends JavaScript to implement these features. TypeScript main features include:

TypeScript is an open source language launched by Microsoft and uses the Apache licensing agreement

TypeScript is a superset of JavaScript.

TypeScript adds optional types, classes and modules

TypeScript compiles to readable, standard JavaScript

TypeScript supports the development of large-scale JavaScript applications

TypeScript is designed for developing large-scale applications and guarantees compatibility of compiled JavaScript code

TypeScript extends the syntax of JavaScript, so existing JavaScript code can run directly with TypeScript without changes

TypeScript file extension is ts, and the TypeScript compiler will compile it into a js file

TypeScript syntax is the same as JScript .NET

TypeScript is easy to learn and understand

Grammatical features

Classes

Interfaces

Modules

Type annotations

Compile time type checking

Arrow function (similar to C#’s Lambda expression)

The difference between JavaScript and TypeScript

TypeScript is a superset of JavaScript that extends the syntax of JavaScript so that existing JavaScript code can work with TypeScript without any modification. TypeScript provides compile-time static type checking through type annotations. TypeScript can process existing JavaScript code and only use

TypeScript code is compiled.

In this section, we will introduce type inference in TypeScript. We'll discuss where type inference is needed and how to do it.

Basics

In TypeScript, type inference will be used to provide type information in several places where type annotations are not explicitly specified.

var x = 3;

The value of variable "x" is inferred to be number. This inference occurs when variables or members are initialized, parameter default values ​​are set, and function return types are determined.

Best Public Type

When type inference is required from multiple expressions, the types of these expressions will be used to infer a "best common type". For example:

var x = [0, 1, null];

To infer the type of "x" in the example, we need to consider the type of each array element. Here, we are given a choice of two array types: number and null. The best common type algorithm requires that all candidate types be considered and a type that is compatible with all candidate types be selected. (The type here can be Array)

Since the best common type is selected from the provided candidate types, there are cases where the candidate types share a common type, but no single type is the parent type of all candidate types. For example:

class Animal {
name:string;
constructor(theName: string) { this.name = theName; }
}
class Snake extends Animal{
constructor(name: string) { super(name); }
}
class Elephant extends Animal{
constructor(name: string) { super(name); }
}
class Rhino extends Animal {
constructor(name: string) { super(name); }
}
var zoo = [new Rhino(), new Elephant(), new Snake()]; // 这里三个成员的类型分别为:Rhino、Elephant、Snake 他们是最佳公共类型的候选类型,Animal是他们的super type(译为父类型) 
登入後複製

Ideally, we might want zoo to be inferred to be of type Animal[], but since no objects in the array are strictly of type Animal, we can't make that inference. To solve this problem, we need to provide the type explicitly when the parent type of all candidate types cannot be inferred.

var zoo: Animal[] = [new Rhino(), new Elephant(), new Snake()]; 
登入後複製

When there is no best public type, the result of inference is to produce an empty object, {}. Because this type contains no members, accessing any of its properties will result in an error. This result still allows us to use the object in a type-ignoring manner, but the type of the object cannot be implicitly determined while ensuring type safety.

Context (Context) Type

In TypeScript, type inference also exists "otherwise" in some cases. This is called "contextual categorization". Contextual collation occurs when the type of an expression is implicitly specified in the context in which it occurs. For example:

window.onmousedown = function(mouseEvent) { 
console.log(mouseEvent.buton); //<- 编译时抛出错误 
}; 
登入後複製

上面的代码将会给出一个类型错误,TypeScript的类型检查器使用Window.onmousedown函数的类型来推断右边的函数表达式类型。当它这么做的时候,便能够推断出参数mouseEvent的类型。 如果这个表达式不在可进行上下文归类的位置,参数mouseEvent 需要给定一个any类型,这样就不会出现错误了。

如果需要上下文归类的表达式内容中包含明确的类型信息,则会忽略上下文归类。我们重写上面的例子:

window.onmousedown = function(mouseEvent: any) { 
console.log(mouseEvent.buton); //<- 现在不会报错了 
}; 
登入後複製

参数明确指定类型的函数表达式将会忽略上下文归类。经过这样的处理就不会报错了,因为没有应用到上下文归类。

上下文归类可应用于许多场景。常见的场景包括函数调用的参数、赋值的等号右边表达式、类型确定、对象成员和数组字面量、返回值语句。上下文类型也作为最佳公共类型的候选类型。例如:

function createZoo(): Animal[] {
return [new Rhino(), new Elephant(), new Snake()];
} 
登入後複製

在这个例子中,最佳公共类型有四个候选类型:Animal,Rhino,Elephant,和Snake。其中,Animal可以作为最佳公共类型。

形式有点像数学中的求最小公倍数...

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!