Typescript mixin constrained to typeof class

Viewed 100

I was following this example from the Typescript documentation:

class Sprite {
  name = "";
  x = 0;
  y = 0;

  constructor(name: string) {
    this.name = name;
  }
}

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

type Spritable = GConstructor<typeof Sprite>;

Next, implemented a function that generates a class extending this type like this:

function Example<TBase extends Spritable>(Base: TBase) {
  return class Example extends Base {
    hello() {
      console.log("hello");
    }
  };
}

I assumed that I would then utilise this Mixin like this:

const x = Example(Sprite);

However, this throws the error

Argument of type 'typeof Sprite' is not assignable to parameter of type 'Spritable'.

If I modify the param passed into the generic constructor to be type Spritable = GConstructor<Sprite>;, the above code works.

However, in the typescript docs it's implemented using typeof which throws me an error. What am I doing wrong here?

1 Answers
Related