[TS04——Interface polymorphism——Generic interface]

  • The polymorphism of the interface, the same method, different parameters are passed in, and the corresponding operations are different to become polymorphic [parameters are different]
  • Or it can be understood as the same method returning different results, which is called polymorphism.
interface IfnPerson {
    
    
    run():void
    run(id:number):void
    run(id:number,name:string):void
}

class Person implements IfnPerson{
    
    
    run():void
    run(id:number):void
    run(id:number,name:string):void
    run(id?:number,name?:string):void{
    
    
        console.log(id,name);   
    }
}

const p  = new Person();
p.run()
p.run(12)
p.run(12,"wwqiew")
interface IfnPerson {
    
    
    run():void
    run(id:number):void
    run(id:number,name:string):void
}

class Person implements IfnPerson{
    
    
    run():void
    run(id:number):void
    run(id:number,name:string):void
    run(id?:number,name?:string):void|string|number{
    
    
        if(arguments.length===0){
    
    
            return "没有参数"
        }else if(id as number>0 && name === undefined){
    
    
            return id;
        }else{
    
    
            return id as number+<string>name
        }
    }
}

const p  = new Person();
p.run()
p.run(12)
console.log(p.run(12,"wwqiew"));

Generic type: a broad type, which refers to a placeholder when it is defined, and the type is not determined, and the type is given when it is called.
When defining a function or class, the type of its parameter or return value cannot be determined at the time of definition. At this time, any or unknown can be used to solve the problem.

The parameter is of any type, and the return is also of any type to avoid the type checking of ts.

function fn(arg:any):any{
    
    
    console.log(arg);
    
    return arg
}
fn(1)
fn("wqw")
fn(false)

generic function

Do not specify a type when defining, and specify a type when returning;

function fn<T>(arg:T):T{
    
    
    console.log(arg);
    
    return arg
}

fn<string>("1")
fn<Array<string>>(["1","ok"])

Instantiate the class - the function of

function fn<T>(classname:{
    
    new():T}){
    
    
    return new classname();
}
//箭头函数写法
//const fn = <T>(classname:{new() :T}):T=>new classname()
class A{
    
    }
class B{
    
    }
// 类也可以作为类型
let a = fn<A>(A);
let b = fn<B>(B);

Generic Type Restriction
Defines a generic class.

class  User<T>{
    
    
    private useList:T[] = []
    push = (value:T)=>{
    
    
        this.useList.push(value)
    }
}

let user = new User<string>();

user.push("a")

console.log(user);


//用接口来规范泛型类
interface Ic{
    
    
    id:number,
    name:string
}

let user = new User<Ic>()
user.push({
    
    id:1,name:"niuniu"})

console.log(user);

Generics can have multiple types. Multiples are separated by commas.

class User<T,K>{
    
    
    name!:T;
    age!:K;
    constructor(name:T,age:K){
    
    
        this.age = age;
        this.name = name;
    }
}

let user =new User("niuniu",25)

Generics can also restrict types.
The current T type must have this attribute in the parent type (interface) specified in extends, otherwise it will be a fart.

interface Ic{
    
    
    length:number
}

function test<T extends Ic>(arg:T){
    
    
    console.log(arg.length);
    return arg.length
}

test({
    
    length:123})
test<string>("wqewq")
test<number[]>([1,2])

insert image description here

// 条件交换
type Excludetype<T,K> = T extends K ? never:T
// 类型的范围限制
type Test = Excludetype<object|string,string|number|boolean>

generic default value

function test<T = number>(arg:T):number{
    
    
    return (arg as unknown as string).length
}
// 可以不写为自动类型
test("1")

class Person<T=string,k=number>{
    
    
    name!:T
    age!:k
}

const p = new Person()

generic interface

When defining an interface, no type is specified.
Specify the type when declaring

interface Isum<T,K>{
    
    
    (a:T,b:K):void
}
const fn:Isum<number,string> = (a:number,b:string)=>{
    
    
    console.log(a,b);       
}

Or
for function use, given the type when calling

interface Isum{
    
    
    <T,K>(a:T,b:K):void
}

const fn:Isum = <T,K>(a:T,b:K):void=>{
    
    
    console.log(a,b);       
}

fn<number,string>(1,"2")

Covariance and contravariance of functions

The return value of a function is covariant, and the function parameter is contravariant.
Covariant: type is self or subclass
contravariant: type is self or superclass

Guess you like

Origin blog.csdn.net/m0_46672781/article/details/127473996