TypeScript practice is essential to note (5) - type compatibility

  TypeScript is a structure type based language, can be of the type described according to its members. In the same configuration and Programmer Interface Person class, for example, as shown below.

interface Person {
  name: string;
}
class Programmer {
  name: string;
}
let person: Person = new Programmer();

  Because of the type of structure, and therefore when a variable is declared as the Person type, the class may be instantiated by the Programmer. It can be seen, only concerned with structure type consisting of structure types, while the name is not important.

First, the function

  When determining the compatibility of the two functions, the number of parameters need to be considered, aspects of return value type.

1) the number of parameters

  Suppose there are two functions add () and SUM (), they have different number of parameters, a parameter which is more than the former, and the same type of function return value, as shown below.

let add = (x: number) => 0;
let sum = (y: number, z: number) => 0;

  When the add assigned sum, it compiled successfully performed; and conversely, an error is reported, as shown below.

the Add = SUM;         // correct 
the Add = SUM;         // error

  It can be seen, few parameters of the function parameters can not be assigned more; while, in turn, need to ensure that each type of parameters corresponding to a position consistent, but may be different parameter name.

2) Return Type

  Suppose there are two functions add () and SUM (), they have no parameters, return value of the latter than the former one attribute, as shown below.

let add = () => ({ x: 1 });
let sum = () => ({ x: 1, y: 2});

  When the sum is assigned to add, to successfully execute the compiler; and conversely, an error is reported, as shown below.

= SUM the Add;         // correct 
SUM = the Add;         // error

  This indicates that the source function of the type of the return value is obtained objective function return value subtype.

3) Parameter Type

  Parameter Type TypeScript the need to meet covariance and, i.e., two-way covariant. Covariant better understood, it refers to a subtype compatible supertype, and coincided with the inverter covariant opposite. In the following example, the definition of the parent class and subclass Person Programmer, Programmer class overrides the work () method of Person class, and its broader parameter type declaration.

class Person {
  work(msg: string | undefined) { }
}
class Programmer extends Person {
  work(msg: string) { }
}

  The next statement two functions, they are the type of parameter and Programmer two Person class, as follows, wherein Person () function is a Programmer () function subtype.

let person = (x: Person) => 0;
let programmer = (x: Programmer) => 0;

  Since the parameter type is a bidirectional covariant, and therefore between the two variables can be assigned to each other, as shown below.

person = programs; 
programs = person;

4) optional parameters, and the remaining parameters

  When the compatibility comparison function, need not match the optional parameters. In the following PLS () and add () function as an example two, PLS () will pass the two parameters, and add () The second parameter is optional.

let pls = (x: number, y: number) => 0;
let add = (x: number, y?: number) => 0;
pls = add;
add = pls;

  Although different parameters, but the two functions are still compatible and can be assigned to each other. The remaining parameters corresponding to an unlimited number of optional parameters, will not be matched. The following examples sum () function only specifies the remaining parameters, it PLS () and add () are two functions compatible.

let sum = (...args: number[]) => 0;
pls = sum;
sum = pls;

add = sum;
sum = add;

5) function overloading

  When comparing multiple overloaded function exists, it must be found on each overload objective function corresponding to the function signature, in order to ensure the objective function can call all the local source function can be called as shown below.

interface add {
  (x: number, y: string): any;
  (x: number, y: number): number;
}
function sum(x: number, y: string): any;
function sum(x: number, y: number): number;
let func: add = sum;

Second, the enumeration

  Enumeration value different from the enumerated type is considered to be incompatible, as shown below, when the color assigned Direction.Up variable at compile error.

enum Color { Red, Green, Blue }
enum Direction { Up, Down, Left, Right }
let color = Color.Red;        //正确
color = Direction.Up;         //错误

  Numbers and numeric enumeration compatible, as shown below, is given a variable Color enumeration members, a variable is numeric digit for, among them can be assigned to each other.

let color = Color.Red;
let digit = 1;
color = digit;
digit = color;

  The string enum is not compatible string type, as shown below, when a variable is assigned to the field enumeration member of Color, at compile time will complain, but in turn can be executed correctly.

enum Color { Red = "RED", Green = "GREEN", Blue = "BLUE" }
let color = Color.Red;
let field = "PURPLE";
color = field;            //错误
field = color;            //正确

Third, the class

  Class and object literal and the interfaces are similar, but the class includes two portions static and instance. When comparing two instances of the class, only matched their instances members, static members and constructor does not affect compatibility, because they are ignored in the comparison.

  In the following example, we create a Programmer Person and two classes, although a Programmer class contains static properties, and which is the Person class constructor different, but they are mutually compatible.

class Person {
  name: string;
  constructor(name: string) { }
}
class Programmer {
  name: string;
  static age: number;
  constructor(name: string, age: number) { }
}

let person: Person;
let programmer: Programmer;
person = programmer;
programmer = person;

  Private and protected members of the class members will affect compatibility, TypeScript requirements they must come from the same class, in order to ensure that the parent class is compatible with both subclasses, but also to avoid compatible with other classes of the same structure.

  In the following example, two classes Teacher Person and contains a private attribute of the same name, is a Person Programmer subclass, three variables corresponding to the type of these three classes.

class Person {
  private name: string;
}
class Teacher {
  private name: string;
}
class Programmer extends Person { }

let person: Person;
let programmer: Programmer;
let teacher: Teacher;

  person and the programmer can assign variables to each other two, because their private member from the same class, as shown below.

person = programs; 
programs = person;

  Although the teacher and the same person two configuration variables, but their private members from two different classes, and therefore can not be assigned to each other, as shown below.

Teacher = the Person;         // error 
Teacher = the Person;         // error

Fourth, Generics

  When the type of the parameter is not used in a generic interface, it does not affect the compatibility, as shown below, x and y are two variables can be assigned to each other.

interface Person<T> { }
let x: Person<number>;
let y: Person<string>;

x = y;
y = x;

  When the type of the parameter is used in the generic interface member, it will affect their compatibility, as shown below, x and y are two variables can not be assigned to each other.

interface Person<T> {
  data: T;
}
let x: Person<number>;
let y: Person<string>;

x = y;        //错误
y = x;        //错误

  When the generic function type comparison parameter is not specified, before checking compatibility will be replaced with any type, for example, the following two functions equivalent to "(x: any) => any" and "(y: any) => any "match, it can be assigned to each other.

let send = function<T>(x: T): T {
  return x;
}
let func = function<U>(y: U): U {
  return y;
}

send = func;
func = send;

   Compatibility rules consistent with the generic class before.

Guess you like

Origin www.cnblogs.com/strick/p/11726848.html