In my TypeScript program I want to extend a class that is declared in a library. The trick is that the library doesn't "export" it so I can not access it directly. Instead, it provides a builder function, like this:
export namespace Library {
class BaseUnexported { // no export here, for some reason
public foo() { console.log("foo"); }
}
export function buildUnexportedInstance(): BaseUnexported {
return new BaseUnexported();
}
}
I'm trying to extend the class like this:
import { Library } from "./library";
export default class Derived extends Library.BaseUnexported {
public bar() { console.log("bar"); }
}
This would work if the library had "exported" the class definition; but without export I get error TS2339: Property 'BaseUnexported' does not exist on type 'typeof Library'.
I tried to obtain the type from the constructor function like:
type BaseType = ReturnType<typeof Library.buildUnexportedInstance>
export default class Derived extends BaseType {
And this time getting error TS2693: 'BaseType' only refers to a type, but is being used as a value here.
So, my question is: is there a way to extend class that is declared without "export" keyword? Maybe some prototype-based magic? Note that my goal is to create a new class, I want to leave the original class unchanged.
P.S. This is a simlified example; in fact I'm trying to extend widgets from a great library called blessed. Just want to create my own widgets which would extend functionality of existing ones.