title: Required vs inferred generic types in TypeScript
body: <p>What is the difference between following generic types:</p>
  
  <pre><code>type FnWithRequiredParam&lt;T&gt; = (t: T) =&gt; void
  type FnWithParamInferred = &lt;T&gt;(t: T) =&gt; void
  </code></pre>
  
  <p>As far as I understand, the <code>FnWithRequiredParam</code> will always fail <em>if not explicitly given</em> the generic type in any context. Passing the generic (which is enforced), e.g. <code>FnWithRequiredParam&lt;string&gt;</code> will basically turn it into <code>(t: string) =&gt; void</code> in all contexts.</p>
  
  <p>However, I can't find the meaning of the <code>FnWithParamInferred</code>. In some contexts <code>&lt;T&gt;</code> gets inferred from the place it's used (such as Array.map), but the following line throws an error:</p>
  
  <pre><code>var f: FnWithParamInferred = (a: number) =&gt; { console.log(a) }
  </code></pre>
  
  <p>saying that <code>number</code> and <code>T</code> are incompatible. In above line, what is actually <code>T</code>? It's never been declared precisely and is being compared to another type. What are the rules for determining what is the generic <code>T</code> defined in function types like <code>&lt;T&gt;(...) =&gt; ...</code>?</p>
  
  <p>Seems that, if <code>&lt;T&gt;</code> is defined as a required generic of a class/interface, e.g. an <code>Array&lt;T&gt;</code>, then methods od array can successfully infer <code>T</code>. But if it's outside of a class/interface, type inference doesn't seem to work.</p>