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

编程入门 行业动态 更新时间:2024-10-20 05:39:50
本文介绍了如何在TypeScript中指定任何可更新的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试过此操作,但不起作用. Foo只是对有效方法的测试. Bar是真正的尝试,它应该可以接收任何新类型,但是Object的子类对此无效.

I tried with this but it doesn't work. Foo is just a test of what works. Bar is the real try, it should receive any newable type but subclasses of Object isn't valid for that purpose.

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; }允许带有构造函数且带有任何参数的任何对象.

You can use { new(...args: any[]): any; } to allow any object with a constructor with any arguments.

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中指定任何可更新的类型?

本文发布于:2023-05-29 23:08:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/355082.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   如何在   TypeScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!