【问题标题】:Make constructor return specific instance type使构造函数返回特定的实例类型
【发布时间】:2021-12-24 19:51:28
【问题描述】:

我正在为公开全局构造函数的第三方库编写 TypeScript 定义文件。

如何编写一个类类型,哪个实例将属于特定类型?

declare class PexRTC {
  // this doesn't work, returned instance is PexRTC, not PexRTC.PexipInstance
  new(): PexRTC.PexipInstance;
}

declare namespace PexRTC {
    export interface PexipInstance {
        foo: true;
    }
}


// How to make instance be PexRTC.PexipInstance ?
const instance = new PexRTC();

Playground

【问题讨论】:

标签: typescript class constructor instance


【解决方案1】:

好的,这就是它的工作方式:

declare const PexRTC: PexRTC.PexipInstance;

declare namespace PexRTC {
  export interface PexipInstance {
    new (): PexRTC.PexipInstance;
  }
}

【讨论】:

  • 不错的解决方案!
猜你喜欢
  • 2016-09-16
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-30
  • 2021-05-17
相关资源
最近更新 更多