【发布时间】:2016-11-14 01:07:27
【问题描述】:
我试图在我的基类上有一个通用工厂方法,它可以实例化任何后代类,而基类不知道所有后代是什么。以下产生工作的JS,但是......
- 尽管有
///<reference>,但我收到 TS 警告(请参阅代码):“typeof MyNS”类型上不存在属性“Base” - 有充足的warnings in the Typescript docs about wrapping modules in namespaces。
- 这种方法似乎只在文件被连接到单个 outFile 时才有效,因为类绑定到
exports的方式(请参阅底部的要点)。这可能是可以接受的,但我很好奇是否有没有这种限制的方法。
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;
}
}
}
生成的 JS:https://gist.github.com/zbjornson/2053cf1a30e893f38f7910dcada712d2
将后代类公开给基类的更好方法是什么?
【问题讨论】:
标签: typescript