2013-07-26 112 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();