简体   繁体   English

Typescript中类型'class'的变量

[英]Variable of type 'class' in Typescript

I'd like to do this: 我想这样做:

createClass(c:class):SomeInstance {
   return new class() as SomeInstance;
}

However, it says 'type expected' where I specify the :class part. 但是,它指的是'type expected',我指定了:class part。

Fixed : 修正:

function createClass<SomeInstance>(c:{new():SomeInstance}):SomeInstance {
   return new c();
}

More 更多

You are basically saying that createClass takes a constructor (something that when called with new gives an instance) hence c:{new():SomeInstance} . 你基本上是说createClass需要一个构造函数(当用new调用时给出一个实例)因此c:{new():SomeInstance} And then the return type of createClass is that SomeInstance 然后createClass的返回类型是SomeInstance

PS: probably call it createInstance 🌹 PS:可能称之为createInstance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM