我试过这个,但它不起作用。 Foo 只是对什么有效的测试。 Bar 是真正的尝试,它应该接收任何可更新的类型,但 Object 的子类对此无效。
class A {
}
class B {
public Foo(newable: typeof A):void {
}
public Bar(newable: typeof Object):void {
}
}
var b = new B();
b.Foo(A);
b.Bar(A); // <- error here
最佳答案
你可以使用 { new(...args: any[]): any; }
允许任何带有带有任何参数的构造函数的对象。
class A {
}
class B {
public Foo(newable: typeof A):void {
}
public Bar(newable: { new(...args: any[]): any; }):void {
}
}
var b = new B();
b.Foo(A);
b.Bar(A); // no error
b.Bar({}); // error
关于typescript - 如何在 TypeScript 中指定任何新类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33224047/