是否有“班级"的类型?在打字稿?并执行“任何"操作包括吗? [英] Is there a type for "Class" in Typescript? And does "any" include it?

查看:54
本文介绍了是否有“班级"的类型?在打字稿?并执行“任何"操作包括吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,您可以使用类"类型将类提供给方法作为参数.我没有在打字稿文档中找到任何类似的东西-是否可以将类传递给方法?如果是这样,"any"类型是否包括此类类类型?

In Java, you can give a class to a method as a parameter using the type "Class". I didn't find anything similar in the typescript docs - is it possible to hand a class to a method? And if so, does the type "any" include such class-types?

背景:我在Webstorm上遇到问题,告诉我不能将类移交给Angular 2中的@ViewChild(...).但是,Typescript编译器没有抱怨. @ViewChild()的签名似乎是"Type<any> | Function | string",所以我想知道是否包含类.

Background: I'm having an issue with Webstorm, telling me that I cannot hand over a class to @ViewChild(...) in Angular 2. However, the Typescript compiler does not complain. The signature of @ViewChild() seems to be "Type<any> | Function | string", so I wonder if any includes Classes or not.

推荐答案

您在打字稿中要求的内容是{ new(): Class }类型,例如:

The equivalent for what you're asking in typescript is the type { new(): Class }, for example:

class A {}

function create(ctor: { new(): A }): A {
    return new ctor();
}

let a = create(A); // a is instanceof A

(上面的代码将只允许其构造函数没有参数的类.如果需要任何课程,请使用new (...args: any[]) => Class

The code above will allow only classes whose constructor has no argument. If you want any class, use new (...args: any[]) => Class

这篇关于是否有“班级"的类型?在打字稿?并执行“任何"操作包括吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆