I am in the process of implementing a generic factory method on my base class that can create instances of any of the descendant classes, without the base class being aware of all the descendants. My current approach generates functional JS code, but...
- Even though I have used
///<reference>
, I am encountering a TS warning (see code): Property 'Base' does not exist on type 'typeof MyNS' - The Typescript documentation contains numerous cautions against enclosing modules within namespaces.
- This method seems to only function properly when the files are merged into a single outFile due to how the classes are linked to
exports
(refer to gist at the end). While this is acceptable, I am interested in exploring an alternative that does not have this limitation.
Base.ts:
export namespace MyNS {
export abstract class Base {
static create(foo) {
return new MyNS[foo.type]();
}
}
}
Descendant.ts:
/// <reference path="Base.ts" />
export namespace MyNS {
// Property 'Base' does not exist on type 'typeof MyNS':
export class Descendant extends MyNS.Base {
echo(s: string) {
return s;
}
}
}
Generated JS code: https://gist.github.com/zbjornson/2053cf1a30e893f38f7910dcada712d2
Is there a more effective way to expose the descendant classes to the base?