2
如果我有两个不同名称空间中的两个类型的文件。生成的订单很重要。Typescript模块作为命名空间
export module Shapes {
export module Weird{
export class weirdshape extends Normal.normalshape{
public x = 3;
}
}
export module Normal{
export class normalshape {
public y = 4;
}
}
}
这将产生
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
define(["require", "exports"], function(require, exports) {
(function (Shapes) {
(function (Weird) {
var weirdshape = (function (_super) {
__extends(weirdshape, _super);
function weirdshape() {
_super.apply(this, arguments);
this.x = 3;
}
return weirdshape;
})(Normal.normalshape);
Weird.weirdshape = weirdshape;
})(Shapes.Weird || (Shapes.Weird = {}));
var Weird = Shapes.Weird;
(function (Normal) {
var normalshape = (function() {
function normalshape() {
this.y = 4;
}
return normalshape;
})();
Normal.normalshape = normalshape;
})(Shapes.Normal || (Shapes.Normal = {}));
var Normal = Shapes.Normal;
})(exports.Shapes || (exports.Shapes = {}));
var Shapes = exports.Shapes;
});
在此为了这将失败。因为Shapes.Normal.normalshape尚未定义。
是否有一个正确的方法来做到这一点在打字稿模块可以用作适当的名称空间?
相信打字稿试图尽可能靠近JavaScript作为可能的,这意味着这是应该在正常的JavaScript崩溃的代码(即上面的代码)将继续在TypeScript中崩溃,而不是让TypeScript引入一些非JavaScript类行为。这确实是增加强大的打字和其他ES6功能,但不做任何流量控制分析。 –
对于您的情况,将两个类分离为单独的模块并使用模块加载器会好得多。模块装载机正是为了解决您的问题而发明的。 –