TypeScript:class 的类型

TypeScript: Type of a class

我只想创建一个以 class 作为参数的函数:

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

但我不知道我必须输入什么类型来指定我正在使用构造函数。

您可以对 return 正确的 class 构造函数使用泛型:

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)