gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 20:54:09 24 4
gpt4 key购买 nike

我试过这个,但它不起作用。 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com