TS Learning 07 - Type Inference

type inference

ts where the type is not explicitly stated, type inference will help provide the type

let x = 3;

Inference occurs when variables and members are initialized , default parameter values ​​are set, and function return values ​​are determined.

best general type

Compute generic type algorithm - will consider all candidate types

gives a type that is compatible with all types

let x = [0,1,null,'123']
when no type can be the type of all candidate types

can be pointed out manually

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

or – type inference results in a union type, (Rhino | Elephant | Snake)[].

context type

TypeScript type inference may also work in the opposite direction. This is called "sorting by context".

Guess you like

Origin blog.csdn.net/weixin_46211267/article/details/132205854