<p dir="auto">Examples:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class C&lt;R&gt;
{
  value : R;
  constructor(v? : R) { this.value = v }
}

// OK, no type error
var a : C&lt;string&gt; = new C(&quot;x&quot;);

// OK, type error
var b : C&lt;string&gt; = new C(1);
var c : C&lt;string&gt; = true ? new C(&quot;x&quot;) : new C(1);
var d : C&lt;string&gt; = new C(1) || new C&lt;string&gt;(null);
function e() : C&lt;string&gt; { if (false) { return new C(null) } else { return new C(1) } }

// Bad, no type error
var f : C&lt;string&gt; = true ? new C(null) : new C(1);
var g : C&lt;string&gt; = true ? new C(1) : new C(null);
var h : C&lt;string&gt; = (() =&gt; { if (false) { return new C(null) } else { return new C(1) } })();
var i : C&lt;string&gt; = new C(1) || new C(null);"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-v">C</span><span class="pl-c1">&lt;</span><span class="pl-ent">R</span><span class="pl-c1">&gt;</span>
<span class="pl-kos">{</span>
  <span class="pl-s1">value</span> : <span class="pl-v">R</span><span class="pl-kos">;</span>
  <span class="pl-en">constructor</span><span class="pl-kos">(</span><span class="pl-s1">v</span>?<span class="pl-s1"></span> : <span class="pl-v">R</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-c1">value</span> <span class="pl-c1">=</span> <span class="pl-s1">v</span> <span class="pl-kos">}</span>
<span class="pl-kos">}</span>

<span class="pl-c">// OK, no type error</span>
<span class="pl-k">var</span> <span class="pl-s1">a</span> : <span class="pl-v">C</span><span class="pl-c1">&lt;</span><span class="pl-ent">string</span><span class="pl-c1">&gt;</span> = new C("x");

// OK, type error
var b : C<span class="pl-c1">&lt;</span><span class="pl-ent">string</span><span class="pl-c1">&gt;</span> = new C(1);
var c : C<span class="pl-c1">&lt;</span><span class="pl-ent">string</span><span class="pl-c1">&gt;</span> = true ? new C("x") : new C(1);
var d : C<span class="pl-c1">&lt;</span><span class="pl-ent">string</span><span class="pl-c1">&gt;</span> = new C(1) || new C<span class="pl-c1">&lt;</span><span class="pl-ent">string</span><span class="pl-c1">&gt;</span>(null);
function e() : C<span class="pl-c1">&lt;</span><span class="pl-ent">string</span><span class="pl-c1">&gt;</span> <span class="pl-kos">{</span> <span class="pl-en">if</span> <span class="pl-kos">(</span><span class="pl-c1">false</span><span class="pl-kos">)</span><span class="pl-kos"></span> <span class="pl-kos">{</span> <span class="pl-s1">return</span> <span class="pl-k">new</span> <span class="pl-v">C</span><span class="pl-kos">(</span><span class="pl-c1">null</span><span class="pl-kos">)</span> <span class="pl-kos">}</span> else <span class="pl-kos">{</span> <span class="pl-s1">return</span> <span class="pl-k">new</span> <span class="pl-v">C</span><span class="pl-kos">(</span><span class="pl-c1">1</span><span class="pl-kos">)</span> <span class="pl-kos">}</span> <span class="pl-kos">}</span>

<span class="pl-c">// Bad, no type error</span>
<span class="pl-k">var</span> <span class="pl-s1">f</span> : <span class="pl-v">C</span><span class="pl-c1">&lt;</span><span class="pl-ent">string</span><span class="pl-c1">&gt;</span> = true ? new C(null) : new C(1);
var g : C<span class="pl-c1">&lt;</span><span class="pl-ent">string</span><span class="pl-c1">&gt;</span> = true ? new C(1) : new C(null);
var h : C<span class="pl-c1">&lt;</span><span class="pl-ent">string</span><span class="pl-c1">&gt;</span> = (() =<span class="pl-c1">&gt;</span> <span class="pl-kos">{</span> <span class="pl-en">if</span> <span class="pl-kos">(</span><span class="pl-c1">false</span><span class="pl-kos">)</span><span class="pl-kos"></span> <span class="pl-kos">{</span> <span class="pl-s1">return</span> <span class="pl-k">new</span> <span class="pl-v">C</span><span class="pl-kos">(</span><span class="pl-c1">null</span><span class="pl-kos">)</span> <span class="pl-kos">}</span> else <span class="pl-kos">{</span> <span class="pl-s1">return</span> <span class="pl-k">new</span> <span class="pl-v">C</span><span class="pl-kos">(</span><span class="pl-c1">1</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>
<span class="pl-k">var</span> <span class="pl-s1">i</span> : <span class="pl-v">C</span><span class="pl-c1">&lt;</span><span class="pl-ent">string</span><span class="pl-c1">&gt;</span> = new C(1) || new C(null);</pre></div>
<p dir="auto">Here, any time <code class="notranslate">new C(1)</code> is used it should have the type <code class="notranslate">C&lt;number&gt;</code> and not unify with <code class="notranslate">C&lt;string&gt;</code>. It seems like <code class="notranslate">R</code> not being known in <code class="notranslate">new C(null)</code> prevents these other type errors from occurring.</p>
<p dir="auto">Courtesy of <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/JsonFreeman/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/JsonFreeman">@JsonFreeman</a></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="function f&lt;T&gt;(t: T): T { return null }
f(null);"><pre class="notranslate"><code class="notranslate">function f&lt;T&gt;(t: T): T { return null }
f(null);
</code></pre></div>
<p dir="auto">When compiling with -noImplicitAny, we don't get an error here, even though T is implicitly inferred to be any when we call f</p>