0

The following is an example of the problem I am experiencing at the moment. I'm trying to write a mixin that properly infers and reflects the generic type from the subclass of the class provided to the mixin.

abstract class A<T = {}> {
  public abstract init(): T;
}

interface IB {
  something: string;
}

class B extends A<IB> {
  public init(): IB {
    return null;
  }
}

type ConstructorOf<T = {}> = new (...args: any[]) => T;

function ApplyC<T extends ConstructorOf<A>>(base: T) {
  abstract class C extends base {

  }
  return C;
}

class D extends ApplyC(B) {
  constructor() {
    super();
    // this.init()
  }
}

Where I written this.init(), the intellisense is telling me that I have an overload on init(), one is init(): {} and the other is init(): IB, the problem with this is that I don't want there to appear to be an overload, and I just want the IB one to appear.

I need the T in A<T = {}> to be inferred somehow in the mixin and pass it to ConstructorOf<A> from the base: T provided.

3

1 Answer 1

0

Do you really need to restrict the ApplyC function to receive only subclasses of A? If no you could replace A with any, e.g.:

    function ApplyC<T extends ConstructorOf<any>>(base: T) {

then D.init() will return just IB and nothing else.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.