I have a hierarchy of class mixins that work in plain JavaScript.
const
AsFoo = ( superclass ) => class extends superclass {
get foo(){ return true; }
},
AsFooBar = ( superclass ) => class extends AsFoo( superclass ){
get bar(){ return true; }
},
FooBar = AsFooBar( Object ),
fb = new FooBar();
console.log( fb.foo, fb.bar );
// true, true
However when I translate them to TypeScript, I get an error with AsFoo( superclass ).
type Constructor<T = {}> = new ( ...args: any[] ) => T;
interface Foo {
foo: boolean;
}
interface FooBar extends Foo {
bar: boolean;
}
const
AsFoo = <T extends Constructor>( superclass: T ): Constructor<Foo> & T => class extends superclass implements Foo {
get foo(){ return true; }
},
AsFooBar = <T extends Constructor>( superclass: T ): Constructor<FooBar> & T => class extends AsFoo<T>( superclass ) implements FooBar {
get bar(){ return true; }
};
// Type 'Constructor<Foo> & T' is not a constructor function type. ts(2507)
Is there something I can do to make TypeScript work with this pattern? I'd rather not just // @ts-ignore: ¯\_(ツ)_/¯ it.
I am currently using TypeScript 3.2.4.
AsFooBarwon't like that you are extending from a generic type parameter. Mixins are a special exception to this rule and they are only allowed in a specific scenario. I would go with something like the answer below..