如何在TypeScript中指定任何新的类型?

Áxe*_*ena 5 method-signature newable typescript

我试过这个,但它不起作用.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
Run Code Online (Sandbox Code Playgroud)

Dav*_*ret 10

您可以使用{ 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
Run Code Online (Sandbox Code Playgroud)