<p dir="auto">If you have multiple classes in the same module namespace but defined in different files (or even just different <code class="notranslate">module</code> blocks) TypeScript's <code class="notranslate">tsc</code> will wrap every one of them into its own nest of IIFEs; one for each level of namespace nesting.</p>
<p dir="auto">For deep namespace hierarchies this produces large amounts of code that increases code size and hurts parsing / loading performance.</p>
<p dir="auto">To solve this problem, we use a post processing tool (<a href="https://github.com/tschneidereit/typescript-module-merger">https://github.com/tschneidereit/typescript-module-merger</a>) to merge subsequent modules that appear in the same namespace. This is not a sound optimization but works well in practice. Such a feature should be part of the TypeScript compiler, perhaps under a compiler flag.</p>
<p dir="auto">We have a typescript application and a use a file per class. There are lots of benefits to this the major one being source control conflict reduction.</p>
<p dir="auto">We compile our app using the --out compiler flag but I think there is room for some serious optimization of the compilers output for modules.</p>
<p dir="auto">Currently if you have two classes defined in separate files the compile generates the following JavaScript.</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="var Example;
(function (Example) {
    (function (Nested) {
        (function (Issue) {
            var bar = (function () {
                function bar() {
                }
                return bar;
            })();
            Issue.bar = bar;
        })(Nested.Issue || (Nested.Issue = {}));
        var Issue = Nested.Issue;
    })(Example.Nested || (Example.Nested = {}));
    var Nested = Example.Nested;
})(Example || (Example = {}));

var Example;
(function (Example) {
    (function (Nested) {
        (function (Issue) {
            var foo = (function () {
                function foo() {
                }
                return foo;
            })();
            Issue.foo = foo;
        })(Nested.Issue || (Nested.Issue = {}));
        var Issue = Nested.Issue;
    })(Example.Nested || (Example.Nested = {}));
    var Nested = Example.Nested;
})(Example || (Example = {}));"><pre class="notranslate"><span class="pl-k">var</span> <span class="pl-v">Example</span><span class="pl-kos">;</span>
<span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-v">Example</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-v">Nested</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-v">Issue</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
            <span class="pl-k">var</span> <span class="pl-s1">bar</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
                <span class="pl-k">function</span> <span class="pl-s1">bar</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
                <span class="pl-kos">}</span>
                <span class="pl-k">return</span> <span class="pl-s1">bar</span><span class="pl-kos">;</span>
            <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
            <span class="pl-v">Issue</span><span class="pl-kos">.</span><span class="pl-c1">bar</span> <span class="pl-c1">=</span> <span class="pl-s1">bar</span><span class="pl-kos">;</span>
        <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-v">Nested</span><span class="pl-kos">.</span><span class="pl-c1">Issue</span> <span class="pl-c1">||</span> <span class="pl-kos">(</span><span class="pl-v">Nested</span><span class="pl-kos">.</span><span class="pl-c1">Issue</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
        <span class="pl-k">var</span> <span class="pl-v">Issue</span> <span class="pl-c1">=</span> <span class="pl-v">Nested</span><span class="pl-kos">.</span><span class="pl-c1">Issue</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-v">Example</span><span class="pl-kos">.</span><span class="pl-c1">Nested</span> <span class="pl-c1">||</span> <span class="pl-kos">(</span><span class="pl-v">Example</span><span class="pl-kos">.</span><span class="pl-c1">Nested</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">var</span> <span class="pl-v">Nested</span> <span class="pl-c1">=</span> <span class="pl-v">Example</span><span class="pl-kos">.</span><span class="pl-c1">Nested</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-v">Example</span> <span class="pl-c1">||</span> <span class="pl-kos">(</span><span class="pl-v">Example</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-k">var</span> <span class="pl-v">Example</span><span class="pl-kos">;</span>
<span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-v">Example</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-v">Nested</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-v">Issue</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
            <span class="pl-k">var</span> <span class="pl-s1">foo</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
                <span class="pl-k">function</span> <span class="pl-s1">foo</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
                <span class="pl-kos">}</span>
                <span class="pl-k">return</span> <span class="pl-s1">foo</span><span class="pl-kos">;</span>
            <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
            <span class="pl-v">Issue</span><span class="pl-kos">.</span><span class="pl-c1">foo</span> <span class="pl-c1">=</span> <span class="pl-s1">foo</span><span class="pl-kos">;</span>
        <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-v">Nested</span><span class="pl-kos">.</span><span class="pl-c1">Issue</span> <span class="pl-c1">||</span> <span class="pl-kos">(</span><span class="pl-v">Nested</span><span class="pl-kos">.</span><span class="pl-c1">Issue</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
        <span class="pl-k">var</span> <span class="pl-v">Issue</span> <span class="pl-c1">=</span> <span class="pl-v">Nested</span><span class="pl-kos">.</span><span class="pl-c1">Issue</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-v">Example</span><span class="pl-kos">.</span><span class="pl-c1">Nested</span> <span class="pl-c1">||</span> <span class="pl-kos">(</span><span class="pl-v">Example</span><span class="pl-kos">.</span><span class="pl-c1">Nested</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">var</span> <span class="pl-v">Nested</span> <span class="pl-c1">=</span> <span class="pl-v">Example</span><span class="pl-kos">.</span><span class="pl-c1">Nested</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-v">Example</span> <span class="pl-c1">||</span> <span class="pl-kos">(</span><span class="pl-v">Example</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">I suggest a compiler flag that composes all classes/ functions etc under a single module declaration? So the output code would be like the example below. This would dramatically reduce the size of the JavaScript the compiler outputs. I estimate that our app which currently compiles to around 15,000 lines of probably be only be 2000 lines if this were available.</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="var Example;
(function (Example) {
    (function (Nested) {
        (function (Issue) {
            var bar = (function () {
                function bar() {
                }
                return bar;
            })();
            Issue.bar = bar;
            var foo = (function () {
                function foo() {
                }
                return foo;
            })();
            Issue.foo = foo;
        })(Nested.Issue || (Nested.Issue = {}));
        var Issue = Nested.Issue;
    })(Example.Nested || (Example.Nested = {}));
    var Nested = Example.Nested;
})(Example || (Example = {}));"><pre class="notranslate"><span class="pl-k">var</span> <span class="pl-v">Example</span><span class="pl-kos">;</span>
<span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-v">Example</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-v">Nested</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-v">Issue</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
            <span class="pl-k">var</span> <span class="pl-s1">bar</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
                <span class="pl-k">function</span> <span class="pl-s1">bar</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
                <span class="pl-kos">}</span>
                <span class="pl-k">return</span> <span class="pl-s1">bar</span><span class="pl-kos">;</span>
            <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
            <span class="pl-v">Issue</span><span class="pl-kos">.</span><span class="pl-c1">bar</span> <span class="pl-c1">=</span> <span class="pl-s1">bar</span><span class="pl-kos">;</span>
            <span class="pl-k">var</span> <span class="pl-s1">foo</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
                <span class="pl-k">function</span> <span class="pl-s1">foo</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
                <span class="pl-kos">}</span>
                <span class="pl-k">return</span> <span class="pl-s1">foo</span><span class="pl-kos">;</span>
            <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
            <span class="pl-v">Issue</span><span class="pl-kos">.</span><span class="pl-c1">foo</span> <span class="pl-c1">=</span> <span class="pl-s1">foo</span><span class="pl-kos">;</span>
        <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-v">Nested</span><span class="pl-kos">.</span><span class="pl-c1">Issue</span> <span class="pl-c1">||</span> <span class="pl-kos">(</span><span class="pl-v">Nested</span><span class="pl-kos">.</span><span class="pl-c1">Issue</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
        <span class="pl-k">var</span> <span class="pl-v">Issue</span> <span class="pl-c1">=</span> <span class="pl-v">Nested</span><span class="pl-kos">.</span><span class="pl-c1">Issue</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-v">Example</span><span class="pl-kos">.</span><span class="pl-c1">Nested</span> <span class="pl-c1">||</span> <span class="pl-kos">(</span><span class="pl-v">Example</span><span class="pl-kos">.</span><span class="pl-c1">Nested</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">var</span> <span class="pl-v">Nested</span> <span class="pl-c1">=</span> <span class="pl-v">Example</span><span class="pl-kos">.</span><span class="pl-c1">Nested</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-v">Example</span> <span class="pl-c1">||</span> <span class="pl-kos">(</span><span class="pl-v">Example</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>