Interfaces and abstract classes were created to add constraints on parameters to standardize code

interface

One of TypeScript’s core tenets is type-checking of the structure a value has. It is sometimes called “duck type discrimination” or “structural typing”. In TypeScript, interfaces name these types and define contracts for your code or third-party code. Duck type discrimination

It's basically: here's this bird, it has methods or properties similar or identical to those of a duck, and it's a duckCopy the code

The keyword interface has some similarities with abstract classes

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

Copy the code

Specifies functions in interfaces

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 Extends is the keyword of an interface

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 needs to be declared immediately and the other does not
Copy the code

An abstract class

In order to specify that some properties and methods in a class are written to be inherited and can only be inherited abstract classes can have some abstract properties and methods that are not implementable. The main difference between an abstract class and an interface is that you can’t implement methods in an interface

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

It can also be implemented using a template

Interface inheritance 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

We also need to inherit Man when we want to use this interface otherwise it is true that a name attribute class inheritance interface implements multiple inheritance of a class using the keyword implements

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

When an interface inherits properties or methods of a class, using the interface also inherits that class

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