0

I simply want to create a function that takes a class as argument:

const mixin = (traits: Object) =>
  (baseClass: class) => { // What type to put here?
    Object.assign(baseClass.prototype, traits)
    return baseClass
  }

But I don't know what type I have to put to specify I'm working with a Constructor Function.

1 Answer 1

4

You can use generics to return the right class constructor:

const mixin = (traits: any) =>
    <T>(baseClass: { new (): T }) => {
        Object.assign(baseClass.prototype, traits);
        return baseClass;
    }

class A {}

let o = mixin({})(A); // typeof o is new () => A
let a = new o(); // typeof a is A

(code in playground)

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

2 Comments

Thanks, that works. But I'm still facing some issues : mixin(Speaker)(class { }) works, but mixin(Speaker)(class extends Mammal { }) does not. Am I missing something?
Sorry, was doing something bad. Works perfectly. Thanks.

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.