Typescript: 构造函数的新对象

创建于 2015-02-19  ·  1评论  ·  资料来源: microsoft/TypeScript

我收到以下错误:
<br i="6"/> TS2351 Cannot use 'new' with an expression whose type lacks a call or construct signature.<br i="7"/>

<br i="10"/> class A {<br i="11"/> constructor() { ... }<br i="12"/> refresh() { return new this.constructor; }<br i="13"/> }<br i="14"/>

新的 this.constructor 在纯 JS 中工作。

谢谢:)

By Design

最有用的评论

不幸的是, constructor的类型只是Function 。 你可以这样写:

class A { constructor() {  } refresh() { return new (<typeof A>this.constructor); } } 

>所有评论

不幸的是, constructor的类型只是Function 。 你可以这样写:

class A { constructor() {  } refresh() { return new (<typeof A>this.constructor); } } 
此页面是否有帮助?
0 / 5 - 0 等级