<p dir="auto">Compiling the code below:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="function identity&lt;T&gt;(arg: T): T {
    return arg;
}

var myIdentity: &lt;T&gt;(arg: T) =&gt; string = identity;

var a = myIdentity&lt;string&gt;(&quot;string&quot;)
var b = myIdentity&lt;number&gt;(100)"><pre class="notranslate"><code class="notranslate">function identity&lt;T&gt;(arg: T): T {
    return arg;
}

var myIdentity: &lt;T&gt;(arg: T) =&gt; string = identity;

var a = myIdentity&lt;string&gt;("string")
var b = myIdentity&lt;number&gt;(100)
</code></pre></div>
<p dir="auto">outputs the following javascript:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="function identity(arg) {
    return arg;
}
var myIdentity = identity;
var a = myIdentity(&quot;string&quot;);
var b = myIdentity(100);"><pre class="notranslate"><code class="notranslate">function identity(arg) {
    return arg;
}
var myIdentity = identity;
var a = myIdentity("string");
var b = myIdentity(100);
</code></pre></div>
<p dir="auto">I put the type signature of the variable <code class="notranslate">myIdentity</code> as <code class="notranslate">&lt;T&gt;(arg: T) =&gt; string</code><br>
where the return type is <code class="notranslate">string</code> instead of <code class="notranslate">T</code> (that I expect to be correct).</p>
<p dir="auto">I expected the Typescript compiler to show an error because the return types<br>
don't match. However, the code compiled without any error.</p>
<p dir="auto">Specs:</p>
<p dir="auto">Mac OS X 10.11</p>
<p dir="auto"><code class="notranslate">tsc --version</code> outputs:<br>
<code class="notranslate">message TS6029: Version 1.6.2</code></p>
<p dir="auto">The following code snippet compiles without type errors though the type variable <code class="notranslate">T</code> is constrained to <code class="notranslate">string</code> type.</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="
function foo1(f: (s: string) =&gt; string)
{
    return f(&quot;hello world&quot;);
}

function foo2(f: (s: number) =&gt; number)
{
    return f(123);
}

function genericBar&lt;T extends string&gt;(arg: T): T
{
    return arg;
}

var x1 = foo1(genericBar);
var x2 = foo2(genericBar);"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">foo1</span><span class="pl-kos">(</span><span class="pl-s1">f</span>: <span class="pl-kos">(</span><span class="pl-s1">s</span>: <span class="pl-smi">string</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi">string</span><span class="pl-kos">)</span>
<span class="pl-kos">{</span>
    <span class="pl-k">return</span> <span class="pl-en">f</span><span class="pl-kos">(</span><span class="pl-s">"hello world"</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-en">foo2</span><span class="pl-kos">(</span><span class="pl-s1">f</span>: <span class="pl-kos">(</span><span class="pl-s1">s</span>: <span class="pl-smi">number</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi">number</span><span class="pl-kos">)</span>
<span class="pl-kos">{</span>
    <span class="pl-k">return</span> <span class="pl-en">f</span><span class="pl-kos">(</span><span class="pl-c1">123</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-en">genericBar</span><span class="pl-c1">&lt;</span><span class="pl-smi">T</span> <span class="pl-k">extends</span> <span class="pl-smi">string</span><span class="pl-c1">&gt;</span><span class="pl-kos">(</span><span class="pl-s1">arg</span>: <span class="pl-smi">T</span><span class="pl-kos">)</span>: <span class="pl-smi">T</span>
<span class="pl-kos">{</span>
    <span class="pl-k">return</span> <span class="pl-s1">arg</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">var</span> <span class="pl-s1">x1</span> <span class="pl-c1">=</span> <span class="pl-en">foo1</span><span class="pl-kos">(</span><span class="pl-s1">genericBar</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">var</span> <span class="pl-s1">x2</span> <span class="pl-c1">=</span> <span class="pl-en">foo2</span><span class="pl-kos">(</span><span class="pl-s1">genericBar</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">Non generic version <code class="notranslate">nonGenericBar</code> works as expected.</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="
function nonGenericBar(arg: string)
{
    return arg;
}

var y1 = foo1(nonGenericBar);
var y2 = foo2(nonGenericBar);  // Type error"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">nonGenericBar</span><span class="pl-kos">(</span><span class="pl-s1">arg</span>: <span class="pl-smi">string</span><span class="pl-kos">)</span>
<span class="pl-kos">{</span>
    <span class="pl-k">return</span> <span class="pl-s1">arg</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">var</span> <span class="pl-s1">y1</span> <span class="pl-c1">=</span> <span class="pl-en">foo1</span><span class="pl-kos">(</span><span class="pl-s1">nonGenericBar</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">var</span> <span class="pl-s1">y2</span> <span class="pl-c1">=</span> <span class="pl-en">foo2</span><span class="pl-kos">(</span><span class="pl-s1">nonGenericBar</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  <span class="pl-c">// Type error</span></pre></div>
<p dir="auto">Of course, <code class="notranslate">genericBar</code> function is useless because constraining a type variable to a primitive type can be replaced by a non generic function like <code class="notranslate">nonGenericBar</code>. However, the behaviour is somewhat unexpected and inconsistent.</p>