Type Hierarchy of TS Booklet Learning

How to determine type compatibility

Use conditional types to determine type compatibility:type Result = 'linbudu' extends string ? 1 : 2;

Compatibility check by assignment (rarely used)

Type-level comparisons have two dimensions

  • type information
  • type system

Due to the nature of the structured type system, we can draw some seemingly contradictory conclusions:

type Result16 = {
    
    } extends object ? 1 : 2; // 1
type Result18 = object extends {
    
    } ? 1 : 2; // 1

type Result17 = object extends Object ? 1 : 2; // 1
type Result20 = Object extends object ? 1 : 2; // 1

type Result19 = Object extends {
    
    } ? 1 : 2; // 1
type Result21 = {
    
    } extends Object ? 1 : 2; // 1

{} extends The and  here  extends {} are actually two completely different ways of comparing.

{} extends object and  {} extends Object means that  {} the literal types of object and Object start from the level of type information , that is, the literal type provides more detailed type information on top of the basic type .

object extends {} And  Object extends {} it starts  from the comparison of structured type systems{} , that is  , as an empty object with nothing, it can almost be regarded as the base class of all types and the origin of all things.

The situation with  these two is special, they are object extends Object because  Object extends object of the "system setting" problem, Object contains all types (basic types, function types, etc.) except Top Type, object contains all non-primitive types Types, that is, arrays, objects, and function types, which lead to the magical phenomenon that there is me in you and you in me.

image.png

Guess you like

Origin blog.csdn.net/jexxx/article/details/128842367