Typescript: mixin compilation error whilst complying with error

53 Views Asked by At

I wrote the following mixin class

export interface IFlyable {
    fly(): void;
}

export function Flyable<TBase extends ObjectConstructor>(Base: TBase) {
    return class extends Base implements IFlyable {
        constructor(...args: any[]) {
            super(...args);
        }

        public fly(): void {
            console.log('I am flying');
        }
    };
}

But my IDE underlines the class keyword with the following message

A mixin class must have a constructor with a single rest parameter of type 'any[]'.ts(2545)

Besides that I am really really not a fan of it, I actually already supplied this constructor in my mixing class. It is the very first piece of code available

constructor(...args: any[]) {
  super(...args);
}

What exactly does the compiler or the language expect of me to resolve this error? I must add that this error does not occur when I change the function declaration to this

export function Flyable<TBase extends Constructor>(Base: TBase)

with Constructor defined as following

interface Constructor {
    new(...args: any[]): Object;
}

I am using Typescript v5.1.3 with the ES2022 specification in my tsconfig.json

enter image description here

0

There are 0 best solutions below