Cleaner module translations


I like how a main focus of the TypeScript team is to keep the JS output as readable as possible. I think it may have been key in TypeScripts success so far.

One thing I've always disliked though, is the translation of:
module ns1.ns2.ns3 {
    // code
var ns1;
(function (ns1) {
    (function (ns2) {
        (function (ns3) {
            // code
        })(ns2.ns3 || (ns2.ns3 = {}));
        var ns3 = ns2.ns3;
    })(ns1.ns2 || (ns1.ns2 = {}));
    var ns2 = ns1.ns2;
})(ns1 || (ns1 = {}));
Thought: I'm not an JS memory expert, but don't the extra closures and additions to the call stack waste memory?

But, more importantly, couldn't this be made more readable by translating it to:
var ns1;
(function( ns1, ns2, ns3) {
    // code
})( ns1 || (ns1 = {}), ns1.ns2 || (ns1.ns2 = {}), ns2.ns3 || (ns2.ns3 = {}) );
You get the advantages of a locally scoped variable, but keep the readable left-to-right-flow of the namespacing.

Now, if in the compiler parses the above TypeScript as:
module ns1 {
    module ns2 {
        module ns3 {
            // code
...I guess it's not an easy fix.
Closed Jul 28 at 10:18 PM by jonturner
As part of our move to GitHub, we're closing our CodePlex suggestions and asking that people move them to the GitHub issue tracker for further discussion. Some feature requests may already be active on GitHub, so please make sure to look for an existing issue before filing a new one.

You can find our GitHub issue tracker here:


danquirk wrote Nov 7, 2013 at 6:45 PM

Marking as a suggestion. There is perhaps a more optimized code gen pattern here but keeping it consistent with the fully expanded form of the module declaration which it is equivalent to is a desirable property as well.