title: What signature should I specify in this generics mess?
body: <p>What signature should <code>ctx</code> argument of <code>loggerOfHidden</code> function have for it to work?</p>
  <p>What <code>'T' could be instantiated with an arbitrary type which could be unrelated to '(Anonymous class)'</code> error means?</p>
  <p>Ignoring errors it outputs <em>just fine</em>.</p>
  <pre><code>Argument of type 'this' is not assignable to parameter of type 'T &amp; Constructor&lt;ISayHidden&gt;'.
    Type '(Anonymous class)' is not assignable to type 'T &amp; Constructor&lt;ISayHidden&gt;'.
      Type '(Anonymous class)' is not assignable to type 'T'.
        'T' could be instantiated with an arbitrary type which could be unrelated to '(Anonymous class)'.
          Type 'this' is not assignable to type 'T'.
            'T' could be instantiated with an arbitrary type which could be unrelated to 'this'.
              Type '(Anonymous class)' is not assignable to type 'T'.
                'T' could be instantiated with an arbitrary type which could be unrelated to '(Anonymous class)'
  </code></pre>
  <pre class="lang-js prettyprint-override"><code>type Constructor&lt;T&gt; = new (...args: any[]) =&gt; T;
  
  interface ISayHidden {
      sayHidden(): void;
  }
  
  class Factory&lt;T extends Constructor&lt;Foo&gt;&gt; {
      constructor(public Base: T) {}
  
      ExtendSayHidden(loggerOfHidden: (ctx: T &amp; Constructor&lt;ISayHidden&gt;) =&gt; void) {
          return new Factory&lt;T &amp; Constructor&lt;ISayHidden&gt;&gt;(class extends this.Base implements ISayHidden {
              sayHidden() {
                  loggerOfHidden(this);
              }
          });
      }
  }
  
  class Foo {
      hiddenMessage = 'Wow, you found me!';
  
      sayHi() {
          console.log('Hi!');
      }
  
      sayBye() {
          console.log('Bye!');
      }
  }
  
  const FooExtended = new Factory(Foo).ExtendSayHidden(ctx =&gt; {
      console.log(ctx.hiddenMessage);
  }).Base;
  
  let fooExtended = new FooExtended();
  
  fooExtended.sayHidden();
  </code></pre>
  <p><strong>EDIT</strong>: the future purpose of <code>loggerOfHidden</code> will be manupulating the extended instance that is being generated by <code>ExtendSayHidden</code> function. Note that <code>Factory</code> can and will contain a multitude of functions, each of which will extend current <code>Base</code> class further. I just want TypeScript to pretend that <code>ctx</code> is no more than <code>Foo</code> extended by the current class, and be able to access these field. I could create a more extensive example if needed.</p>
  <p><strong>EDIT2</strong>: Updated example. Can TypeScript provide <code>ctx</code> with the context of the current class?</p>
  <pre class="lang-js prettyprint-override"><code>type Constructor&lt;T&gt; = new (...args: any[]) =&gt; T;
  
  interface ISayHidden {
      hiddenMessage: string;
      sayHidden(): void;
  }
  
  class Factory&lt;T extends Constructor&lt;Foo&gt;&gt; {
      constructor(public Base: T) {}
  
      ExtendSayHidden(loggerOfHidden: &lt;U extends Foo&gt;(ctx: U) =&gt; void) {
          return new Factory&lt;T &amp; Constructor&lt;ISayHidden&gt;&gt;(class extends this.Base implements ISayHidden {
              hiddenMessage = 'Wow, you found me!';
  
              sayHidden() {
                  loggerOfHidden(this);
              }
          });
      }
  }
  
  class Foo {
      sayHi() {
          console.log('Hi!');
      }
  
      sayBye() {
          console.log('Bye!');
      }
  }
  
  const FooExtended = new Factory(Foo)
  .ExtendSayHidden(ctx =&gt; {
      console.log(ctx.hiddenMessage);
  }).Base;
  
  let fooExtended = new FooExtended();
  
  fooExtended.sayHidden();
  </code></pre>