TypeScript type innference (type inference)

Source: Internet
Author: User

In this section, we will describe the type inference in typescript. We will discuss where the type inference needs to be used and how to infer it.

Basis

In typescript, type inference is used to provide type information in several places where there are no explicitly specified type annotations.

var x = 3;

The value of the variable "x" is inferred as number. This inference occurs when a variable or member is initialized, sets the default value of the parameter, and determines the return type of the function.

Best public type

When you need type inference from multiple expressions, the types of these expressions will be used to infer a "best public type." For example:

var null];

To infer what type of "X" is 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 a type compatible with all candidate types is selected. (The type here can be array<number>)

Because the best public types are selected from the provided candidate types, in some cases the candidate types share a common type, but none of the types is the parent type of all candidate types. For example:

class Animal {    name: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 = [newnewnew///  Here The three members are of the following types: Rhino, Elephant, Snake  They are candidate types of the best public type, animal is their super type (translated to parent type)

Ideally, we might want the zoo to be inferred as a animal[] type, but since none of the objects in the array is a strict animal type, we cannot make inferences. To solve this problem, we need explicit supply types when we cannot infer the parent type of all candidate types.

var zoo:animal[] = [newnew new Snake ()];

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 will result in an error. This result still allows us to use the object in a way that ignores the type, but the type of the object cannot be implicitly determined under the premise of guaranteeing type safety.

Context (context) type

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

function (mouseEvent) {     console.log (Mouseevent.buton);   // <-Throw 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 the expression is not in a context-sensitive position, the parameter mouseevent needs to be given an any type, so there is no error.

If the expression content that requires context collation contains explicit type information, the context collation is ignored. Let's rewrite the example above:

function (mouseevent:any) {     console.log (Mouseevent.buton);   // };

A function expression that explicitly specifies the type of the parameter ignores the context collation. This processing will not result in an error, because it is not applied to the context collation.

Context collations can be applied to many scenarios. Common scenarios include parameters for a function call, an equal-right expression for an assignment, a type determination, an object member and an array literal, and a return value statement. The context type also acts as the candidate type for the best public type. For example:

function Createzoo (): animal[] {    return [new New New Snake ()];

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

form a bit like the least common multiple in mathematics ...

TypeScript type innference (type inference)

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.