【发布时间】:2014-09-11 17:46:13
【问题描述】:
我在一个模块中有一个类。
module Foo {
export class Bar {
}
/* Some more interfaces and classes to be used only in this module */
}
var bar = new Foo.Bar();
这个类是一个库,我不希望其他用户编写 Foo.Bar 而只是 Bar 来使用我的 Bar 类:
var bar = new Bar();
我可以通过定义一个新变量 Bar 来做到这一点。
var Bar = Foo.Bar;
var bar = new Bar();
但是,现在我有一个问题。我不能将 Bar 用作 TypeScript 类型标识符。
function something(bar: Bar) { // Compiler: Could not find symbol 'Bar'.
}
我也可以通过定义一个扩展 Foo.Bar 的新类来解决这个问题。
class Bar extends Foo.Bar {
}
var bar = new Bar();
function something(bar: Bar) {
}
但是,生成的 Bar 类与 Foo.Bar 并不完全相同,因为 Bar === Foo.Bar 和 Bar.prototype === Foo.Bar.prototype 都返回 false。
我试图找到一种使用 TypeScript 模块功能(例如 import 和 require)的方法,但我似乎无法使用它们。有什么好的方法可以将我的 Bar 类公开给全局吗?
【问题讨论】:
标签: typescript