Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

TypeScript type inference


May 07, 2021 TypeScript



TypeScript Type Inference Introduction

This section introduces the type inferences in TypeScript. That is, where and how the type is inferred.

Basis

In TypeScript, where the type is not explicitly identified, type inference helps provide the type. As in the example below

let x = 3;

The type of variable x is inferred as a number. This inference occurs when variables and members are initialized, default parameter values are set, and function return values are determined.

In most cases, type inferences are straightforward. In the next section, we look at the nuances of type inference.

The best generic type

When you need to infer a type from several expressions, the types of those expressions are used to infer a generic type that is most appropriate. For example

let x = [0, 1, null];

In order to infer the type of x we must consider the type of all elements. T here are two options: number and null The calculation common type algorithm considers all candidate types and gives a type that is compatible with all candidate types.

Because the final generic type is taken from the candidate type, there are times when the candidate type shares the same generic type, but none of the types can be used as all candidate types. For example:

let zoo = [new Rhino(), new Elephant(), new Snake()];

Here, we want zoo to be inferred as Animal[] but no object in this Animal so we can't infer this result. To correct, we need to clearly indicate the type when the candidate type is not available:

let zoo: Animal[] = [new Rhino(), new Elephant(), new Snake()];

If the best generic type is not found, the result of type inference is an {} Because this type does not have any members, errors are reported when accessing its members.

The context type

TypeScript type inferences can also go in the opposite direction. T his is called "categorization by context". C ategorization by context occurs when the type of expression is related to where it is located. Like what:

window.onmousedown = function(mouseEvent) {
    console.log(mouseEvent.buton);  //<- Error
};

This example results in a type error, and the TypeScript type inspector uses Window.onmousedown infer the type of the function expression on the right. T herefore, you can infer mouseEvent parameter. If the function expression is not at the location of the context type, the type of the mouseEvent argument needs to be any so that no mistakes are reported.

If the context type expression contains explicit type information, the type of the context is ignored. Rewrite the example above:

window.onmousedown = function(mouseEvent: any) {
    console.log(mouseEvent.buton);  //<- Now, no error is given
};

This function expression has explicit argument type annotations and context types are ignored. That would be a mistake, because the context type is not used here.

Context classization is used in many cases. T ypically contains the function's arguments, the right side of the assignment expression, type assertions, object members and arrays literally, and return value statements. C ontext types are also considered candidates for the best generic types. Like what:

function createZoo(): Animal[] {
    return [new Rhino(), new Elephant(), new Snake()];
}

In this example, there are four candidates for the best generic type: Animal Rhino Elephant and Snake Of course, Animal is considered the best generic type.