TS: Type of inference compatible

TS: Type of inference compatible

1. Variable types compatible

  • If the variable x is to be compatible with y, then y must have at least the same attributes as x:

    interface Named {
          
          
        name : string;
    }
    let x : Named;
    let y = {
          
          
        name : 'yivi',age : 12
    };
    x = y;	// ok,y中拥有x的属性name
    

2. Compatible function types

  • The first is parameter compatibility: depending on whether the function x can be assigned to y, each parameter of x must be able to find the corresponding type of parameter in y . (The parameter name does not matter, the type is the same).

    let x = (a: number) => 0;
    let y = (b: number , s:string) => 0;
    y = x;	// ok
    x = y;	//error,x中缺少y的多余参数。
    
  • The second is the return value compatibility: the return value type of the original function must be a subtype of the return value type of the target function.

    let x = () => ({
          
          name : 'yivi'});
    let y = () => ({
          
          name : 'yivi',age : 20});
    
    x = y;	// ok
    y = x; 	// error,x的返回值缺少参数。
    

3. Instability of optional parameters and remaining parameters

  • When a function has remaining parameters, it will be treated as an unlimited number of optional parameters;

  • This is unstable for the type system, but from an operational point of view, it is equivalent to a pass-through path for most functions undefined.

  • The parameters in the callback function are predictable to the programmer, but uncertain to the type system.

    function foo(args : any[],callback : (...args : any[]) => void){
          
          
        // 程序实现
    }
    

4. Enumeration compatible

  • Enumeration types and digital types are compatible with each other;

  • Different enumeration types are incompatible with each other;

    enum A {
          
          OKERROR};
    enum B {
          
          SUCCESS,FAILED};
    
    let status = A.OK;
    status = B.SUCCESS;	//error!
    

5. Class compatibility

  • When comparing objects of two class types, only the members of the instance will be compared.

  • Static members and constructors are not included! ! !

    class Cat {
          
          
        kind : string;
        constructor(name : string,size : number){
          
          }
    }
    class Dog {
          
          
        kind : string;
        constructor(name : string){
          
          }
    }
    
    let c : Cat;
    let d : Dog;
    c = d;	//ok
    d = c;	//ok
    

6. Generic compatibility

  • The effect of the type parameter uses its result type as part of the type.

    interface Empty<T> {
          
          }
    
    let x : Empty<number>;
    let y : Empty<string>;
    
    x = y;	//ok,由于接口里没有成员,所以y的结构和x一致
    
  • But when there are members in the interface, it is a different matter:

    interface Full<T> {
          
          
        data : T
    }
    
    let x : Full<number>;
    let y : Full<string>;
    
    x = y;	// error!x和y的结构不一致!
    

Guess you like

Origin blog.csdn.net/yivisir/article/details/109586083