在 Java 中,你可以给出一个 class to a method作为使用类型“Class”的参数。我没有在 typescript 文档中找到任何类似的东西——是否可以将一个类交给一个方法?如果是这样,“任何”类型是否包括此类类类型?
背景:我在使用 Webstorm 时遇到问题,告诉我无法将类(class)交给 @ViewChild(...)
在 Angular 2 中。但是,Typescript 编译器不会提示。 @ViewChild()
的签名好像是"Type<any> | Function | string"
, 所以我想知道是否有任何包含类。
最佳答案
你在 typescript 中要求的等价物是类型 { new(): Class }
,例如:
class A {}
function create(ctor: { new(): A }): A {
return new ctor();
}
let a = create(A); // a is instanceof A
上面的代码只允许构造函数没有参数的类。如果你想要任何类,使用 new (...args: any[]) => Class
关于angular - Typescript 中有 "Class"的类型吗? "any"是否包括它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39392853/