简体   繁体   English

打字稿中的类构造函数类型?

[英]Class constructor type in typescript?

How can I declare a class type, so that I ensure the object is a constructor of a general class?如何声明class类型,以确保该对象是通用类的构造函数?

In the following example, I want to know which type should I give to AnimalClass so that it could either be Penguin or Lion :在下面的例子中,我想知道我应该给AnimalClass哪种类型,以便它可以是PenguinLion

class Animal {
    constructor() {
        console.log("Animal");
    }
}

class Penguin extends Animal {
    constructor() {
        super();
        console.log("Penguin");
    }
}

class Lion extends Animal {
    constructor() {
        super();
        console.log("Lion");
    }
}

class Zoo {
    AnimalClass: class // AnimalClass could be 'Lion' or 'Penguin'

    constructor(AnimalClass: class) {
        this.AnimalClass = AnimalClass
        let Hector = new AnimalClass();
    }
}

Of course, the class type does not work, and it would be too general anyway.当然, class类型不行,反正也太笼统了。

Solution from typescript interfaces reference :来自打字稿接口参考的解决方案:

interface ClockConstructor {
    new (hour: number, minute: number): ClockInterface;
}
interface ClockInterface {
    tick();
}

function createClock(ctor: ClockConstructor, hour: number, minute: number): ClockInterface {
    return new ctor(hour, minute);
}

class DigitalClock implements ClockInterface {
    constructor(h: number, m: number) { }
    tick() {
        console.log("beep beep");
    }
}
class AnalogClock implements ClockInterface {
    constructor(h: number, m: number) { }
    tick() {
        console.log("tick tock");
    }
}

let digital = createClock(DigitalClock, 12, 17);
let analog = createClock(AnalogClock, 7, 32);

So the previous example becomes:所以前面的例子变成了:

interface AnimalConstructor {
    new (): Animal;
}

class Animal {
    constructor() {
        console.log("Animal");
    }
}

class Penguin extends Animal {
    constructor() {
        super();
        console.log("Penguin");
    }
}

class Lion extends Animal {
    constructor() {
        super();
        console.log("Lion");
    }
}

class Zoo {
    AnimalClass: AnimalConstructor // AnimalClass can be 'Lion' or 'Penguin'

    constructor(AnimalClass: AnimalConstructor) {
        this.AnimalClass = AnimalClass
        let Hector = new AnimalClass();
    }
}

Like that:像这样:

class Zoo {
    AnimalClass: typeof Animal;

    constructor(AnimalClass: typeof Animal ) {
        this.AnimalClass = AnimalClass
        let Hector = new AnimalClass();
    }
}

Or just:或者只是:

class Zoo {
    constructor(public AnimalClass: typeof Animal ) {
        let Hector = new AnimalClass();
    }
}

typeof Class is the type of the class constructor. typeof Class是类构造函数的类型。 It's preferable to the custom constructor type declaration because it processes static class members properly.它比自定义构造函数类型声明更可取,因为它可以正确处理静态类成员。

Here's the relevant part of TypeScript docs .这是TypeScript docs的相关部分。 Search for the typeof .搜索typeof As a part of a TypeScript type annotation, it means "give me the type of the symbol called Animal" which is the type of the class constructor function in our case.作为 TypeScript 类型注释的一部分,它的意思是“给我一个名为 Animal 的符号的类型”,在我们的例子中,它是类构造函数的类型。

I am not sure if this was possible in TypeScript when the question was originally asked, but my preferred solution is with generics :当最初提出问题时,我不确定这是否可以在TypeScript中实现,但我的首选解决方案是使用泛型

class Zoo<T extends Animal> {
    constructor(public readonly AnimalClass: new () => T) {
    }
}

This way variables penguin and lion infer concrete type Penguin or Lion even in the TypeScript intellisense.这样,即使在 TypeScript 智能感知中,变量penguinlion推断出具体的PenguinLion类型。

const penguinZoo = new Zoo(Penguin);
const penguin = new penguinZoo.AnimalClass(); // `penguin` is of `Penguin` type.

const lionZoo = new Zoo(Lion);
const lion = new lionZoo.AnimalClass(); // `lion` is `Lion` type.

How can I declare a class type, so that I ensure the object is a constructor of a general class?如何声明类类型,以确保该对象是通用类的构造函数?

A Constructor type could be defined as:构造函数类型可以定义为:

 type AConstructorTypeOf<T> = new (...args:any[]) => T;

 class A { ... }

 function factory(Ctor: AConstructorTypeOf<A>){
   return new Ctor();
 }

const aInstance = factory(A);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM