title: Generic constraint when defining a type
body: <p>I am trying to come up with a type for a function with a single callback parameter (also a function) such that the type of the parameter of that callback is restricted to <em>object</em> only.</p>
  
  <p>However I am getting a type error. Here is the code:</p>
  
  <pre class="lang-js prettyprint-override"><code>function aaa(a: { n: number }) { }
  
  type Cb = &lt;T extends object&gt;(a: T) =&gt; void
  function ccc(fn: Cb) { }
  // type error - why?
  ccc(aaa)
  </code></pre>
  
  <p>Type error:</p>
  
  <pre><code>Argument of type '(a: { n: number; }) =&gt; void' is not assignable to parameter of type 'Fn'.
    Types of parameters 'a' and 'a' are incompatible.
      Type 'T' is not assignable to type '{ n: number; }'.
        Type 'object' is not assignable to type '{ n: number; }'.
          Property 'n' is missing in type '{}'.
  </code></pre>
  
  <p>A similar generic constraint, but applied to a function definition, works fine:</p>
  
  <pre class="lang-js prettyprint-override"><code>function aaa(a: { n: number }) { }
  
  function bbb&lt;T extends object&gt;(fn: (a: T) =&gt; void) { }
  // all good
  bbb(aaa)
  </code></pre>
  
  <p>What is the difference between the two? And how can I get the former one to work?</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p><a href="https://www.typescriptlang.org/play/#src=function%20aaa(a%3A%20%7Bn%3A%20number%7D)%20%7B%20%7D%0D%0A%0D%0Afunction%20bbb(fn%3A%20(a%3A%20object)%20%3D%3E%20void)%20%7B%20%7D%0D%0A%0D%0Abbb(aaa)" rel="nofollow noreferrer">playground</a> link</p>