1 Interface definition

You define a new type specification that says you have to have a variable or method, so you have to have it when you use a function or class to define it as an interface type.

Function people(params:{name:string}):void {console.log(params.name)} people({name:' small '}) interface people {  name:string; age:number; sex? Function Name(params:People):void {console.log(params) console.log(params.name+params.age+params.sex) } let para = {name:' zhang3 ', age:18, sex:' male '} name (para)Copy the code

2 Function type interface

interface People { (name:string,age:number):string } var people:People = function (name:string,age:number):string { Return name+age} people(' zhang3 ',18)Copy the code

3 Constraints on an array of indexable interfaces Constraints on an object are not commonly used

interface People { [index:number]:string } var arr:People=['111','222'] interface People { [index:string]:string } var Arr :People={name:'zaz', sex:' male '}Copy the code

Indexable interfaces can of course be used to pass arbitrary arguments to functions, but with the limitation that in cases like the following the argument must be an arbitrary subclass

Interface People {namme:string, [index:any]:string} interface People {namme:string, [index:any]:stringCopy the code

4 Class types Interfaces and abstract classes are similar to the keyword implements

interface People {

    name:string;

    eat(str:string):void

   }

class Heiren implements People {

    name:string

    constructor(name:string) {

        this.name = name

    }

    eat(str:string){}

}
Copy the code

5 Interface Extension Interface inheritance

interface Animal { eat():void; } interface People extends Animal{ run():void } class Web implements People { public name:string; Constructor (constructor) {this.name=name} run():void{console.log(this.name+" run ")} eat():void{console.log(this.name+" run ")} Console. log(this.name+" eat ")}} var p = new Web(' small ') p.run() p.eat() name:string; Constructor (string) {this.name=name} look(): constructor(string) {console.log(this.name+" long eyes ")}} class extends Father Implements People {constructor(name:string) {super(name)} run():void{console.log(this.name+" run ") super.look()} Eat ():void{console.log(this.name+" will eat ")}} var son = new son (' flower ') son.run() son.eat()Copy the code