<p dir="auto">Given the following two files in a project:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="//app.ts
enum MyEnum {
    one,
    two,
    three
}"><pre class="notranslate"><span class="pl-c">//app.ts</span>
<span class="pl-k">enum</span> <span class="pl-smi">MyEnum</span> <span class="pl-kos">{</span>
    <span class="pl-c1">one</span><span class="pl-kos">,</span>
    <span class="pl-c1">two</span><span class="pl-kos">,</span>
    <span class="pl-c1">three</span>
<span class="pl-kos">}</span></pre></div>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="//class.ts
namespace MyNamespace {
    //class must be exported for bug 
    export class MyClass {
        private message = &quot;hello&quot;;

        log() {
            //setTimeout must be returned, not just called
            return setTimeout(() =&gt; console.log(this.message));
        }
    }
}"><pre class="notranslate"><span class="pl-c">//class.ts</span>
<span class="pl-k">namespace</span> <span class="pl-smi">MyNamespace</span> <span class="pl-kos">{</span>
    <span class="pl-c">//class must be exported for bug </span>
    <span class="pl-k">export</span> <span class="pl-k">class</span> <span class="pl-smi">MyClass</span> <span class="pl-kos">{</span>
        <span class="pl-k">private</span> <span class="pl-c1">message</span> <span class="pl-c1">=</span> <span class="pl-s">"hello"</span><span class="pl-kos">;</span>

        <span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
            <span class="pl-c">//setTimeout must be returned, not just called</span>
            <span class="pl-k">return</span> <span class="pl-en">setTimeout</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">message</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></pre></div>
<p dir="auto">The generated JavaScript for <code class="notranslate">MyClass.log</code> will erroneously reference <code class="notranslate">this</code> instead of <code class="notranslate">_this</code> inside of the <code class="notranslate">setTimeout</code> method, i.e. it will emit:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="MyClass.prototype.log = function () {
    return setTimeout(function () { return console.log(this.message); });
};"><pre class="notranslate"><span class="pl-v">MyClass</span><span class="pl-kos">.</span><span class="pl-c1">prototype</span><span class="pl-kos">.</span><span class="pl-en">log</span> <span class="pl-c1">=</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">return</span> <span class="pl-en">setTimeout</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">return</span> <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">message</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-kos">;</span></pre></div>
<p dir="auto">instead of:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="MyClass.prototype.log = function () {
    var _this = this;
    return setTimeout(function () { return console.log(_this.message); });
};"><pre class="notranslate"><span class="pl-v">MyClass</span><span class="pl-kos">.</span><span class="pl-c1">prototype</span><span class="pl-kos">.</span><span class="pl-en">log</span> <span class="pl-c1">=</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">var</span> <span class="pl-s1">_this</span> <span class="pl-c1">=</span> <span class="pl-smi">this</span><span class="pl-kos">;</span>
    <span class="pl-k">return</span> <span class="pl-en">setTimeout</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">return</span> <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s1">_this</span><span class="pl-kos">.</span><span class="pl-c1">message</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-kos">;</span></pre></div>
<p dir="auto">Here are the conditions necessary for this to happen. They may not be the <em>only</em> conditions necessary (every time I think I have this bug nailed another condition emerges), so if you cannot reproduce it let me know and I will send a full project that exhibits the bug.</p>
<ul dir="auto">
<li>"Compile on save" must be enabled</li>
<li>"Combine JavaScript output into file" must be enabled, with a file specified (say, <code class="notranslate">out.js</code>)</li>
<li>"Generate declaration files" must be enabled</li>
<li><code class="notranslate">MyClass</code> must be exported from a namespace</li>
<li><code class="notranslate">setTimeout</code> must be <em>returned</em> from <code class="notranslate">MyClass.log</code>, not just called</li>
<li>The output of <code class="notranslate">app.ts</code> must come before <code class="notranslate">class.ts</code> in the combined output file. I believe this is satisfied by the fact that "app.ts" is alphabetically before "class.ts".</li>
</ul>
<p dir="auto">If these conditions are satisfied, and if you alter <code class="notranslate">app.ts</code> <em>without</em> altering <code class="notranslate">class.ts</code> and save, the <code class="notranslate">out.js</code> that is generated in response to that save will have the erroneous code.</p>
<p dir="auto">This is using Visual Studio 2015 and TypeScript 1.8.</p>
<p dir="auto"><strong>TypeScript Version:</strong><br>
1.8.0 (through npm), 1.8.2 (visual studio)</p>
<p dir="auto"><strong>Code</strong></p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="namespace Bug {

    export interface IDef {
        test2: () =&gt; any;
        test: (e:any) =&gt; any;
    }

    class TestController {
        protected from: number;

        public prepareDefinition(def: IDef) {
            return def;
        }

        public prepared = this.prepareDefinition({
            test2: () =&gt; {
                var q = this.from; // uses _this;
                var helper = () =&gt; {
                    var q2 = this.from; // uses _this
                };
                return {
                    fn1: (v) =&gt; {
                        var q3 = this.from; //uses this
                    },
                    fn2: helper
                }
            },
            test: (e) =&gt; { }
        } 
        );


    }

}"><pre class="notranslate"><span class="pl-k">namespace</span> <span class="pl-smi">Bug</span> <span class="pl-kos">{</span>

    <span class="pl-k">export</span> <span class="pl-k">interface</span> <span class="pl-smi">IDef</span> <span class="pl-kos">{</span>
        <span class="pl-c1">test2</span>: <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi">any</span><span class="pl-kos">;</span>
        <span class="pl-c1">test</span>: <span class="pl-kos">(</span><span class="pl-s1">e</span>:<span class="pl-smi">any</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi">any</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>

    <span class="pl-k">class</span> <span class="pl-smi">TestController</span> <span class="pl-kos">{</span>
        <span class="pl-k">protected</span> <span class="pl-c1">from</span>: <span class="pl-smi">number</span><span class="pl-kos">;</span>

        <span class="pl-k">public</span> <span class="pl-en">prepareDefinition</span><span class="pl-kos">(</span><span class="pl-s1">def</span>: <span class="pl-smi">IDef</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
            <span class="pl-k">return</span> <span class="pl-s1">def</span><span class="pl-kos">;</span>
        <span class="pl-kos">}</span>

        <span class="pl-k">public</span> <span class="pl-c1">prepared</span> <span class="pl-c1">=</span> <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-en">prepareDefinition</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
            <span class="pl-en">test2</span>: <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
                <span class="pl-k">var</span> <span class="pl-s1">q</span> <span class="pl-c1">=</span> <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">from</span><span class="pl-kos">;</span> <span class="pl-c">// uses _this;</span>
                <span class="pl-k">var</span> <span class="pl-en">helper</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
                    <span class="pl-k">var</span> <span class="pl-s1">q2</span> <span class="pl-c1">=</span> <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">from</span><span class="pl-kos">;</span> <span class="pl-c">// uses _this</span>
                <span class="pl-kos">}</span><span class="pl-kos">;</span>
                <span class="pl-k">return</span> <span class="pl-kos">{</span>
                    <span class="pl-en">fn1</span>: <span class="pl-kos">(</span><span class="pl-s1">v</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
                        <span class="pl-k">var</span> <span class="pl-s1">q3</span> <span class="pl-c1">=</span> <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">from</span><span class="pl-kos">;</span> <span class="pl-c">//uses this</span>
                    <span class="pl-kos">}</span><span class="pl-kos">,</span>
                    <span class="pl-c1">fn2</span>: <span class="pl-s1">helper</span>
                <span class="pl-kos">}</span>
            <span class="pl-kos">}</span><span class="pl-kos">,</span>
            <span class="pl-en">test</span>: <span class="pl-kos">(</span><span class="pl-s1">e</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</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-kos">}</span></pre></div>
<p dir="auto">The code is compiled to ES5<br>
<strong>Expected behavior:</strong><br>
All the 3 assignments (q, q2, q3) should compile to using _this</p>
<p dir="auto"><strong>Actual behavior:</strong><br>
The q3 assignment refers to "this". The code above (with the exception of the helper(), which I kept for comparison) seems to be the minimal code to trigger the issue - even removing test() or its parameters (or parameter of fn1) results in what I believe is the correct code (using _this). The code also compiles correctly in 1.7.x</p>