2013-07-26 77 views
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尚未定义。

是否有一个正确的方法来做到这一点在打字稿模块可以用作适当的名称空间?

+1

相信打字稿试图尽可能靠近JavaScript作为可能的,这意味着这是应该在正常的JavaScript崩溃的代码(即上面的代码)将继续在TypeScript中崩溃,而不是让TypeScript引入一些非JavaScript类行为。这确实是增加强大的打字和其他ES6功能,但不做任何流量控制分析。 –

+1

对于您的情况,将两个类分离为单独的模块并使用模块加载器会好得多。模块装载机正是为了解决您的问题而发明的。 –

回答

1

因此,如果问题是“如何使这个代码运行”,答案是这样:

export module Shapes { 
    export module Normal{ 
     export class normalshape { 
      public y = 4; 
     } 
    } 

    export module Weird{ 
     export class weirdshape extends Normal.normalshape{ 
      public x = 3; 
     } 
    } 
} 

这是不是真的打字稿的限制 - 这是JavaScript的一个限制。如果你在声明它之前使用了某些东西,你会遇到问题。

您可能会争辩说,TypeScript应该为您排序,因为它可以解决依赖关系。实际上,如果你有单独的文件,它会这样做。例如,如果NormalNormal.tsWeirdWeird.ts,生成的输出将为您正确排序。

完整示例:

Weird.ts

/// <reference path="Normal.ts" /> 

module Shapes { 
    export module Weird { 
     export class weirdshape extends Shapes.Normal.normalshape { 
      public x = 3; 
     } 
    } 
} 

Normal.ts

module Shapes { 
    export module Normal { 
     export class normalshape { 
      public y = 4; 
     } 
    } 
} 

app.ts

/// <reference path="Weird.ts" /> 
/// <reference path="Normal.ts" /> 

var weird = new Shapes.Weird.weirdshape(); 

使用--out final.js编译 - 所得final.js

var Shapes; 
(function (Shapes) { 
    (function (Normal) { 
     var normalshape = (function() { 
      function normalshape() { 
       this.y = 4; 
      } 
      return normalshape; 
     })(); 
     Normal.normalshape = normalshape; 
    })(Shapes.Normal || (Shapes.Normal = {})); 
    var Normal = Shapes.Normal; 
})(Shapes || (Shapes = {})); 
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 __(); 
}; 
var Shapes; 
(function (Shapes) { 
    (function (Weird) { 
     var weirdshape = (function (_super) { 
      __extends(weirdshape, _super); 
      function weirdshape() { 
       _super.apply(this, arguments); 
       this.x = 3; 
      } 
      return weirdshape; 
     })(Shapes.Normal.normalshape); 
     Weird.weirdshape = weirdshape; 
    })(Shapes.Weird || (Shapes.Weird = {})); 
    var Weird = Shapes.Weird; 
})(Shapes || (Shapes = {})); 
var weird = new Shapes.Weird.weirdshape();