Class 类型脚本中的类构造函数类型?

Class 类型脚本中的类构造函数类型?,class,typescript,types,constructor,Class,Typescript,Types,Constructor,如何声明类类型,以确保对象是一般类的构造函数 在下面的示例中,我想知道我应该给AnimalClass指定哪种类型,以便它可以是Penguin或Lion: class Animal { constructor() { console.log("Animal"); } } class Penguin extends Animal { constructor() { super(); console.log("Penguin")

如何声明
类型,以确保对象是一般类的构造函数

在下面的示例中,我想知道我应该给
AnimalClass
指定哪种类型,以便它可以是
Penguin
Lion

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();
    }
}
当然,
类型不起作用,无论如何它都太通用了。

解决方案来自:

因此,前面的示例变成:

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();
    }
}
就像这样:

class Zoo {
    AnimalClass: typeof Animal;

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

class Zoo {
    constructor(public AnimalClass: typeof Animal ) {
        let Hector = new AnimalClass();
    }
}
typeof类
是类构造函数的类型。它比自定义构造函数类型声明更可取,因为它正确地处理静态类成员

这是本书的相关部分。搜索的
类型。作为TypeScript类型注释的一部分,它的意思是“给我一个名为Animal的符号类型”,在我们的例子中,它是类构造函数的类型

如何声明类类型,以确保对象是一般类的构造函数

构造函数类型可以定义为:

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

 class A { ... }

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

const aInstance = factory(A);
type AConstructorTypeOf=new(…args:any[])=>T;
A类{…}
函数工厂(Ctor:AConstructorTypeOf){
返回新的Ctor();
}
常数=工厂(A);

我不确定最初提出问题时在TypeScript中是否可以这样做,但我首选的解决方案是使用泛型:

class动物园{
构造函数(公共只读AnimalClass:new()=>T){
}
}
这样,变量
penguin
lion
甚至在TypeScript intellisense中也可以推断具体的类型
penguin
lion

const企鹅动物园=新动物园(企鹅);
const penguin=new penguinZoo.AnimalClass();//`企鹅是企鹅型的。
const lionZoo=新动物园(狮子);
const lion=new lionZoo.AnimalClass();//`“狮子”是“狮子”类型。

我只想补充一点,这个解决方案对我不起作用,如果你想使用一个参数数不为零的构造函数,它也不会起作用。我花了很长时间才弄明白为什么这个例子有效,但我的代码却没有。经过数小时的搜索,我发现使用了
AnimalClass:typeof Animal
。这将适用于动态加载给定类的子类。@pixelpax您可以定义一个非零参数构造函数,如下所示:
new(…args:any[]):Animal
@pixelpax它没有意义,因为
typeof Animal
只是字符串
“function”
。任何课程都将满足这一要求,而不仅仅是动物因素。@arthur.sw,请查看以下链接。它可以工作,但是编译器不应该编译最后一行
,因为typeof Animal只是字符串“function”
——在普通的JS中,是的。在TypeScript类型注释中,no.
typeof X
表示符号X的类型,不管它是什么,编译器正在静态处理它。
typeof
返回字符串,不能使用结果调用
new
typeof Animal
是一个“返回”的TypeScript类型注释动物构造函数的类型。它不返回字符串。在尝试更正答案之前,最好在typescript中运行有问题的代码并查阅文档()。永远不会受伤:)。比公认的答案更好、更直观!不幸的是,使用此方法会丢失构造函数参数的类型检查。
 type AConstructorTypeOf<T> = new (...args:any[]) => T;

 class A { ... }

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

const aInstance = factory(A);