<p dir="auto"><strong>TypeScript Version:</strong></p>
<p dir="auto">nightly (1.9.0-dev.20160429)</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="interface Factory&lt;T&gt; {
    (): T;
    mixin&lt;U&gt;(mixin: U): T &amp; U;
}

function compose&lt;T&gt;(base: T): Factory&lt;T&gt; {
    return;
}

const factory = compose({
    foo() {
        return this;
    },
    bar() {
        this.foo(); // &lt;-- properly contextual and literal union type
        return () =&gt; {
            return this; // &lt;-- type `any`, but should be same as outside the lambda
        };
    }
});

const foobar = {
    foo() {
        return this;
    },
    bar() {
        return () =&gt; {
            return this; // &lt;-- correct object literal type
        };
    }
};"><pre class="notranslate"><span class="pl-k">interface</span> <span class="pl-smi">Factory</span><span class="pl-c1">&lt;</span><span class="pl-smi">T</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-smi">T</span><span class="pl-kos">;</span>
    <span class="pl-c1">mixin</span><span class="pl-c1">&lt;</span><span class="pl-smi">U</span><span class="pl-c1">&gt;</span><span class="pl-kos">(</span><span class="pl-s1">mixin</span>: <span class="pl-smi">U</span><span class="pl-kos">)</span>: <span class="pl-smi">T</span> <span class="pl-c1">&amp;</span> <span class="pl-smi">U</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">function</span> <span class="pl-en">compose</span><span class="pl-c1">&lt;</span><span class="pl-smi">T</span><span class="pl-c1">&gt;</span><span class="pl-kos">(</span><span class="pl-s1">base</span>: <span class="pl-smi">T</span><span class="pl-kos">)</span>: <span class="pl-smi">Factory</span><span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-k">return</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">const</span> <span class="pl-s1">factory</span> <span class="pl-c1">=</span> <span class="pl-en">compose</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
    <span class="pl-en">foo</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">this</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos">,</span>
    <span class="pl-en">bar</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// &lt;-- properly contextual and literal union type</span>
        <span class="pl-k">return</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">return</span> <span class="pl-smi">this</span><span class="pl-kos">;</span> <span class="pl-c">// &lt;-- type `any`, but should be same as outside the lambda</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-k">const</span> <span class="pl-s1">foobar</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span>
    <span class="pl-en">foo</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">this</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos">,</span>
    <span class="pl-en">bar</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-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
            <span class="pl-k">return</span> <span class="pl-smi">this</span><span class="pl-kos">;</span> <span class="pl-c">// &lt;-- correct object literal type</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"><strong>Expected behavior:</strong></p>
<p dir="auto">That both references to <code class="notranslate">this</code> are typed the same.</p>
<p dir="auto"><strong>Actual behavior:</strong></p>
<p dir="auto">I am pretty sure this is related to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="151696359" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/8356" data-hovercard-type="pull_request" data-hovercard-url="/microsoft/TypeScript/pull/8356/hovercard" href="https://github.com/microsoft/TypeScript/pull/8356">#8356</a>.</p>
<p dir="auto">Outside of the lambda function in the first object literal, <code class="notranslate">this</code> is an intersection type of the object literal and the contextual type (which happen to be the same).  In the second example, <code class="notranslate">this</code> is properly the object literal type including within the scope of the lambda function.</p>
<p dir="auto">On the docket</p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="130412950" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/6784" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/6784/hovercard" href="https://github.com/microsoft/TypeScript/issues/6784">#6784</a> - Destructuring a property of an optional property should be an error</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="129775713" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/6723" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/6723/hovercard" href="https://github.com/microsoft/TypeScript/issues/6723">#6723</a> - Allow separate outDir for declaration files</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="129279252" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/6678" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/6678/hovercard" href="https://github.com/microsoft/TypeScript/issues/6678">#6678</a> - Use <code class="notranslate">const</code> initializer to resolve tuple type indexing</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="129201313" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/6649" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/6649/hovercard" href="https://github.com/microsoft/TypeScript/issues/6649">#6649</a> - Do not suggest <code class="notranslate">caller</code> in strict mode</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="128310962" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/6585" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/6585/hovercard" href="https://github.com/microsoft/TypeScript/issues/6585">#6585</a> - Making <code class="notranslate">typings</code> in <code class="notranslate">package.json</code> an array</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="126640496" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/6480" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/6480/hovercard" href="https://github.com/microsoft/TypeScript/issues/6480">#6480</a> - Allow <code class="notranslate">static</code> index signatures in classes</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="125573803" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/6395" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/6395/hovercard" href="https://github.com/microsoft/TypeScript/issues/6395">#6395</a> - Provide a way of specifying type parameters in JSX tags</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="120273102" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/5916" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/5916/hovercard" href="https://github.com/microsoft/TypeScript/issues/5916">#5916</a> - Disallow <code class="notranslate">return;</code> statements when a return type annotation exists</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="112662964" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/5352" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/5352/hovercard" href="https://github.com/microsoft/TypeScript/issues/5352">#5352</a> - Disallow empty binding patterns</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="126668998" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/6482" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/6482/hovercard" href="https://github.com/microsoft/TypeScript/issues/6482">#6482</a> - Support a <code class="notranslate">declarationPath</code> option</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="127560984" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/6544" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/6544/hovercard" href="https://github.com/microsoft/TypeScript/issues/6544">#6544</a> - Allow narrowing to a constituent of an intersection type</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="124781924" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/6344" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/6344/hovercard" href="https://github.com/microsoft/TypeScript/issues/6344">#6344</a> - Allow elided type arguments at call sites</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="128486808" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/6606" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/6606/hovercard" href="https://github.com/microsoft/TypeScript/issues/6606">#6606</a> - Apply <code class="notranslate">typeof</code> to arbitrary expressions</li>
</ul>
<p dir="auto">Stretch goals</p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="121578889" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/6041" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/6041/hovercard" href="https://github.com/microsoft/TypeScript/issues/6041">#6041</a> - Resolve the indexer mess</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="128618856" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/6613" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/6613/hovercard" href="https://github.com/microsoft/TypeScript/issues/6613">#6613</a> - Better typing for React.setState</li>
</ul>