Make constructor return specific instance type

Viewed 39

I'm writing TypeScript definitions file for a third-party library that exposes a global constructor function.

How do I write a class type which instance will be of a specific type?

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

1 Answers

Ok , so this is how to make it work:

declare const PexRTC: PexRTC.PexipInstance;

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