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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39614311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:20:22  来源:igfitidea点击:

Class constructor type in typescript?

classtypescripttypesconstructor

提问by arthur.sw

How can I declare a classtype, 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 AnimalClassso that it could either be Penguinor 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 classtype does not work, and it would be too general anyway.

class类型当然不行,反正也太笼统了。

采纳答案by Nenad

I am not sure if this was possible in TypeScriptwhen 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 penguinand lioninfer concrete type Penguinor Lioneven in the TypeScript intellisense.

这样的变量penguinlion推断具体类型Penguin或者Lion甚至在打字稿智能感知。

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.

回答by arthur.sw

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();
    }
}

回答by gaperton

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 Classis the type of the class constructor. It's preferable to the custom constructor type declaration because it processes static class members properly.

typeof Class是类构造函数的类型。它比自定义构造函数类型声明更可取,因为它可以正确处理静态类成员。

Here's the relevant part of TypeScript docs. Search for the 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 docs的相关部分。搜索typeof. 作为 TypeScript 类型注释的一部分,它的意思是“给我一个名为 Animal 的符号的类型”,在我们的例子中,它是类构造函数的类型。

回答by AleC

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);