Typescript type innference (type judgment) _javascript tips

Source: Internet
Author: User

Typescript is a superset of JavaScript developed by Microsoft, Typescript compatible with JavaScript, and can load JavaScript code and run it. Typescript is a step forward compared to JavaScript: adding annotations, allowing the compiler to understand the objects and functions that are supported, the compiler removes annotations, does not add overhead, and adds a complete class structure to update the traditional object-oriented language.

Why would there be typescript?

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

Typescript is Microsoft's Open source language, using the Apache licensing protocol

Typescript is a hyper-set of JavaScript.

Typescript adds optional types, classes, and modules

Typescript can be compiled into readable, standard JavaScript

Typescript supports the development of large-scale JavaScript applications

Typescript designed to develop large applications and ensure the compatibility of compiled JavaScript code

Typescript extends JavaScript syntax, so existing JavaScript code can run directly with typescript without changing

The typescript file name extension is TS, and the typescript compiler is compiled into a JS file

Typescript syntax is the same as JScript. NET

Typescript easy to learn and easy to understand

Syntax characteristics

Class Classes

interface interfaces

Module Modules

Type annotation type annotations

Compile-time type check Compile times type checking

Arrow function (LAMBDA expression similar to C #)

The difference between JavaScript and typescript

Typescript is a superset of JavaScript that extends the syntax of JavaScript, so existing JavaScript code can work with typescript without any modification, TYPESCRIPT provides compile-time static type checking through type annotations. Typescript can handle existing JavaScript code, and only the

Typescript code to compile.

In this section, we will introduce the type inference in typescript. We will discuss where and how the type inference needs to be inferred.

Basis

In typescript, type inference is used to provide type information in a few places where the type annotation is not explicitly specified.

var x = 3;

The value of the variable "x" is inferred to be number. This inference occurs when a variable or member is initialized, the default value of a parameter is set, and the function return type is determined.

Best public type

When type inference is required from multiple expressions, the type of these expressions is used to infer a "best public 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 give a selection of two array types: number and null. The best common type algorithm requires that all candidate types be taken into account and that the types compatible with all candidate types are selected. (The type here can be array<number>)

Because the best public type is chosen from the candidate type provided, in some cases the candidate type shares a common type, but none of the types are the parent types 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);}
}

Ideally, we might want zoo to be inferred as a animal[] type, but because there are no objects in the array that are strictly animal types, we cannot infer. To solve this problem, we need to explicitly provide a type when it is not possible to infer all of the candidate type's parent types.

 
 

When there is no best public type, the inferred result is an empty object, {}. Because this type does not contain any members, access to any of its properties can result in an error. This result still allows us to use the object in a way that ignores the type, but it cannot be implicitly determined if the type security is guaranteed.

Context (context) type

In typescript, type inference also exists in "other aspects" in some cases. This is called a "contextual collation." A context collation occurs when the type of an expression is implicitly specified in the context in which it is located. For example:

Window.onmousedown = function (mouseevent) { 
console.log (Mouseevent.buton);//<-throws an error at compile time 

The above code will give a type error, and the Typescript type Checker uses the type of the Window.onmousedown function to infer the function expression type on the right. When it does so, it can infer the type of the parameter MouseEvent. If this expression is not in a position where the context can be categorized, the parameter mouseevent needs to give an any type so that there is no error.

If the content of an expression that requires a context collation contains explicit type information, the context collation is ignored. We rewrite the example above:

Window.onmousedown = function (mouseevent:any) { 
console.log (Mouseevent.buton);//<-no more errors now. 

A function expression that explicitly specifies the type of a parameter ignores the context collation. This process does not cause an error because it is not applied to the context collation.

Context collations can be applied to many scenarios. Common scenarios include the arguments to function calls, the expression to the right of an assignment, type determination, object members and array literals, and return value statements. Context types also serve as candidate types for the best public types. For example:

function Createzoo (): animal[] {return
[New Rhino (), New Elephant (), New Snake ()];

In this example, the best public type has four candidate types: animal,rhino,elephant, and Snake. Where animal can be the best public type.

The form is somewhat like LCM in mathematics ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.