具有称为“新”功能的Typescript接口

时间:2019-08-20 15:01:29

标签: typescript types interface

我正在尝试为具有工厂功能(称为“新”)的外部库编写TS定义。问题在于,在接口中添加一个名为“ new”的函数是指构造函数的类型。

有什么办法可以逃脱新关键字或使这种类型正常工作?

export interface Container {

  //This is what I want, except as a regular function, not the constructor
  new<T>(TConstructor: new () => T, locals?: object): T;

}

2 个答案:

答案 0 :(得分:4)

添加一对引号就足够了:

export interface Container {

  //This is what I want, except as a regular function, not the constructor
  'new'<T>(TConstructor: new () => T, locals?: object): T;

}

答案 1 :(得分:2)

您只需引用方法标识符:

export interface Container {

  'new'<T>(TConstructor: new () => T, locals?: object): T;

}

declare let o: Container;
o.new(null!)

Play