Be present in someone else’s life like a gift!

What is an interface type, interface?

. Interfaces are also a type that constrain users in the same way as number, String, Boolean, enum data types. Something is a kind of describe objects | | function (which can be understood as a shape). An object to the above properties. What arguments the function takes or what values it returns. What the array should look like. Object interface function interface indexable interface Class interface and interface inheritance definition mixed type…

Object type interface

interface FullName{ The requirement defines a function that outputs a person's full name. The person's last name must be a string, and the first name must also be a string
        firstName: string;     
        lastName: string;
    }
    let obj = {
        firstName: "Next door".lastName: "Wang"        // An error is reported if it is not a string, such as lastName: 18
    }
                                //{farstName,lastName} uses destruct assignment
    fucntion say({firstName,lastName}:FullName):void{
        console.log(` I call:${farstName}+${lastName}`) 
    }
    say(obj)                   // My name is Lao Wang
Copy the code

* You can disable this rule in tsLint. Add the following rule to rules: “interface-name”:[true,”never-prefix”]. The shape of the variable must be the same as that of the interface. Is the interface definable? 1 Optional properties 2 Read-only properties 3 Arbitrary properties make the interface more flexible

Define interface strong constraints
 interface Istate {    
        name:String.age:number
    }
    var obj1:Istate;
    obj1 = {name:"Zhang".age:10}
Copy the code
Optional attribute questionable
interface Istate2 {
    name: string,        // Common properties Set common properties that need to existage? : number,// Optional attributes are optional? Note that the optional attribute gets undefined if it is not assignedsex? : number,/ /...studay? () :void       // Is the optional method optional? // The optional method must be checked before calling otherwise an error will be reported
}         
var obj2: Istate2;
obj2 = {name:"Zhang".age:20}
obj2 = {name:"Horse six"}    // The age is not assigned
console.log(obj2.sex)   // If the optional attribute is not assigned, the value obtained is undefined
console.log(obj2.studay)// Cannot be called this way because it may be an undefined object
                        // Call the method correctly
if(obj2.studay){obj2.studay()}                               

Copy the code
Read-only properties (that is, properties defined in the interface are readable and writable by default)
interface Istate {
    name: string,
readonly age: number        // A read-only attribute is an initial value that you do not want to change later
}
var obj4:istate = {name:"Zhao four".age:10}
obj4.name = "Fool"
obj4.age = 20           // An error is reported because it is already in the read-only state
Copy the code
Any type
// If the number of attributes is uncertain, any must be of any type
// then I dynamically insert the added one
interface Istate3 {
name:string|number,     / / joint type So the name can be a string | numberage? :number,// The age attribute in question can be given or not
[propName:string]:any   Additional attributes that can be accepted can also be specified by as or [propName: string]: any
}        
var obj3:Istate3 = {name:"Zhang".age:10.sex:"Male".isMarry:true}
Copy the code
Defines methods in the interface
// Defining interfaces can have methods as well as attributes
  interface Ipersson {
      name: String;
      run(): void;
      eat(): void;
  }
  If we have an object of the interface type, we must include the corresponding properties and methods
  const p: Iperson = {
      name: "why".run(){
          console.log("That's one way.")},eat(){
          console.log("This is a method too")}}Copy the code

Big guy please go away, if there is a mistake or not rigorous place, please leave a note, thank you very much, to the author is also a kind of encouragement.