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

2023-11-25

我尝试过这个,但它不起作用。 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 TypeScript 中指定任何可更新的类型? 的相关文章

随机推荐