Interfaces and abstract classes appear primarily to add constraints on parameters and to regulate code

interface

One of TypeScript’s core principles is type checking of the structure that values have. It is sometimes called “duck typing” or “structural typing”. In TypeScript, interfaces are used to name these types and define contracts for your code or third-party code. Duck form method

Basically: here's this bird, it has similar or the same methods or properties as a duck, and it's a duckCopy the code

The keyword interface and abstract class have some similarities

interface Eg {
	a: number; // Required parametersb? : string;// This parameter is optionalreadonly c? : string;// This parameter is read-only
    [prop:number]:boolean // The index value should be of type number and the value should be a Boolean value
}
var data: Eg = { a: 1 ,b:"2".c:"3".1:true}
console.log(data)

Copy the code

Functions are specified in the interface

interface Eg {
    (x:string,y:string): string // Specify the function
}
let eg: Eg;
eg = function (x:string,y:string) :string {
    return "eg"
}
Copy the code

Interface Inheritance Interface keyword: extends

interface Shape {
    color: string;
}

interface PenStroke {
    penWidth: number;
}

interface Square extends Shape, PenStroke {
    sideLength: number;
}
let square = <Square>{}; 
let square:Square = {} // error 
// Both of these mean using the Square interface
// The difference is that one is assigned immediately after declaration and the other is not
Copy the code

An abstract class

In order to specify that there are some properties and methods in a class that are written to be inherited and can only be inherited abstract classes can’t be instantiated and abstract classes can have some abstract properties and methods and the main difference between an interface and an interface is that an interface can’t implement a method whereas an abstract class can use the keyword abstract

abstract class Human {
    abstract move(x: number): any;
    constructor (readonly name:string) {}
    eat(){}}class Student extends Human {
    move(x:number) {
        // Abstract methods inherited from abstract classes must be implemented in derived classes
    }
    constructor (name:string) {
        super(name)
    }
}
Copy the code
abstract class Human<T> {
    abstract move(x: number):T;
    constructor (readonly name:string) {}
    eat(){}}Copy the code

This can also be done using a class

Interface inheriting class:

class Man {
    job: string;
    constructor (readonly name: string) {}
    earnMoney () {
        console.log(`earning money`)
    }
}
 
interface HumanRule extends Man{
    nose: string;
    mouth: string;
    ear: string;
    eye: string;
    eyebrow: string
}
Copy the code

So when we’re going to use this interface we’re going to inherit from Man or else we’re going to have a name property and we’re going to inherit from the interface and we’re going to use the keyword implements implements as well

class a extends Man implements HumanRule{
    nose: string;
    mouth: string;
    ear: string;
    eye: string;
    eyebrow: string;
    constructor (name) {
    	super(name)
    }
}
Copy the code

This means that when an interface inherits a property or method from a class, the interface inherits that class as well

Mixed type

interface Counter {
    (start: number): string;
    interval: number;
    reset(): void;
}

function getCounter() :Counter {
    let counter = <Counter>function (start: number) {}; counter.interval =123;
    counter.reset = function () {};return counter;
}
let c = getCounter();
c(10);
c.reset();
c.interval = 5.0;
Copy the code