01 interface

/ /! interface
/ /! Is an abstraction (description) of an object's state (properties) and behavior (methods)
/ /! It's a type, it's a norm or a rule or a constraint or a capability
/ * * *! Object of interface type *! Optional properties:? *! Read-only property: readonly */
(() = > {
  // * Requirement: create the object of the person, need to restrict the attributes of the person;
  // *id is of type number and must be read-only;
  // *name is a string;
  // *age = number;
  // *sex is a string;
  // Todo defines an interface that is used as the Person object type to qualify or constrain the object property data
  interface IPerson {
    //* Readonly THE ID is read-only
    readonly id: number;
    name: string;
    age: number;
    / / *? Optional attributesex? : string; }// Todo defines an object whose type is the interface I defined
  const person: IPerson = {
    id: 11.name: "45".age: 15.// sex: "don't know ",
  };
  console.log("person: ", person); }) ();Copy the code

02 Type of the function

/ /! Type of function
/ /! Used as a function type through an interface
/ /! It is like a function definition with only the argument list and return value types. Each parameter in the parameter list needs a name and type.
(() = > {
  // Define a function type to be used as the type of a function
  interface ISearchFun {
    // * Define a call signature
    (source: string, sub: string): boolean;
  }
  // * Define a function whose type is the interface above
  const searchString:ISearchFun = function (source: string, sub: string) :boolean {
    // Find sub in source
    return source.search(sub) > -1;
  };
  //* Call the function
  console.log(searchString("Ha ha ha."."Oh")); }) ();Copy the code

03 class type

/ /! Class type: The type of a class can be implemented through an interface
(() = > {
  / /? Define an interface
  interface IFly {
    //* This method does not have any implementation (nothing)
    fly(): any;
  }
  / /? Define a class. The interface defined above on the type of this class (in fact, IFly constrains the current Person class)
  class Person implements IFly {
    / /? Implement methods in the interface
    fly() {
      console.log("Look at me"); }}/ /? Instantiate an object
  const person = new Person();
  person.fly();

  interface ISwim {
    swim(): any;
  }
  // Todo defines a class whose types are IFly and ISwim (currently this class can implement multiple interfaces, and a class can be constrained by multiple interfaces)

  class Person2 implements IFly.ISwim {
    fly() {
      console.log("Look at me. I'm number two.");
    }
    swim() {
      console.log("Look at me. I'm number two."); }}const person2 = new Person2();
  person2.fly();
  person2.swim();

  / /! Class defines the type of the current class through an interface
  / /! A class can implement one interface, or a class can implement multiple interfaces. Note that the content in the interface must be truly implemented
  / /! Interfaces can inherit multiple interfaces from others

  // Defines an interface and inherits the above interface
  interface IWan extends Person2, Person {}

  // Define a class that implements IFly and ISwim directly
  class Person3 implements IWan {
    fly() {
      console.log("Look at me. I'm three.");
    }
    swim() {
      console.log("Look at me. I'm number three."); }}const person3 = new Person3();
  person3.fly();
  person3.swim();

  / /! Interfaces and interfaces are called inheritance (using extends)
  / /! Between a class and an interface is called implementation (implements is used)}) ();Copy the code