1. Type assertion

Type assertion is like conversion in other languages, but without special data checking and deconstruction. It has no run-time impact, only at compile time, and TypeScript assumes that the programmer has already checked it

Type assertions come in two ways:

1.1 Angle bracket writing method

let someValue: any = "this is a string";
// The compiler can't find the length attribute if the type is any
let strLength: number = (<string>someValue).length;
Copy the code

1.2 aswriting

let someValue: any = "this is a string";
let strLength: number = (someValue as string).length;
Copy the code

The two forms are equivalent, but when JSX syntax is used in TypeScript, only as syntax assertions are allowed


2. Type inference

In TypeScript, type inference helps provide types where they are not explicitly specified

let x = 3;
// The type of variable x is inferred to be a number. This inference occurs when initializing variables and members, setting default parameter values, and determining the return value of a function
Copy the code

2.1 Best generic type

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

let x = [0.1.null]; / / when x is inferred as (null | number) []
Copy the code

Because the final generic type is derived from the candidate type, sometimes the candidate types share the same generic type, but no one type can be used as a type for all candidate types

class Animal {}
class Rhino extends Animal {}
class Elephant extends Animal {}
class Snake extends Animal {}

let zoo = [new Rhino(), new Elephant(), new Snake()];
/ / be extrapolated for (Rhino | Elephant | Snake) [] type
// We want zoo to be inferred as Animal[], but there are no objects in this array that are Animal, so we can't infer this result
// For correction, you need to specify the type explicitly
let zoo: Animal[] = [new Rhino(), new Elephant(), new Snake()];
Copy the code

2.2 Context Type

TypeScript type inference can also go in the opposite direction. This is called “categorizing by context.” Context-specific categorization occurs when the type of the expression is related to its location

window.onmousedown = function(mouseEvent) { // This example will report an error
    console.log(mouseEvent.button);  // -> Error
};
/* The TypeScript type checker uses the type of the window.onmousedown function to infer the type of the right-hand function expression. Therefore, you can infer the type of the mouseEvent parameter. If the function expression is not in the context type, the mouseEvent parameter needs to be of type any so that no errors are reported
window.onmousedown = function(mouseEvent: any) { // Manually specifying the parameter type the context type is ignored
    console.log(mouseEvent.button);  // <- Now, no error is given
};
Copy the code

Contextual categorization can be used in many situations. Usually contains function arguments, the right side of assignment expressions, type assertions, object member and array literals, and return value statements. Context types are also considered candidates for the best generic type

function createZoo() :Animal[] { // In this case Animal is the best generic type
    return [new Rhino(), new Elephant(), new Snake()];
}
Copy the code