1

我正在努力定义如何编写 TypeScipt 代码,该代码表示​​该函数返回泛型类型的构造函数。有很多关于如何传递泛型类型的构造函数的例子,但没有关于如何返回的例子。

请检查以下示例:

这是抽象类的一部分:

 getModel():  (new () => T) {
    throw new Error('Method not implemented.'); // Error because don't know how to fix it
}

在派生类中,我试图像这样实现它:

getModel(): typeof User {
    return User;
}

我有以下错误:

Type '() => typeof User' is not assignable to type '() => new () => User'.

如果我知道如何在抽象类中指定,我可以跳过派生类中的实现。

所以问题是 - 如何在抽象类级别指定该方法返回泛型类型的构造函数,并且我可以在子级别类中跳过此方法的实现?或者我在抽象类级别上没有正确指定返回签名?

编辑:

请检查奇怪的问题。A 类和 B 类的区别仅在于显式构造函数的存在。在 RealA 中不起作用,RealB 使用相同的 getModel() 方法。

class A {
a = '';
constructor(a: string) {

}
}

class B {
    a = '';
    static test(): void {
        console.log('I do work');
    }
}

abstract class Base<T> {
    Prop: T;
    constructor(TCreator: { new (): T; }) {
        this.Prop = new TCreator();
    }

    getModel(): (new () => T) {
        throw new Error('Method not implemented.'); // Error because don't know how to fix it
    }
}

class RealA extends Base<A> {
    getModel(): typeof A { // doesn't work - compilation error
        return A;
    }
}

class RealB extends Base<B> {
    getModel(): typeof B { // works
        return B;
    }
}

var test = new RealA(A); // compile error
var test2 = new RealB(B)

对于 RealA 类同样的错误

() => typeof A' is not assignable to type '() => new () => A'
4

1 回答 1

1

该错误是预期的,因为类的构造函数A具有必需的参数。抽象类将构造函数限制为没有参数 ( new () => T)。

简单的解决方案是将构造函数移除到A.

如果您希望能够传入具有需要参数的构造函数的类,您将需要更改基类的定义以捕获构造函数类型,并constructor接受这些必需的参数(在其余参数中使用元组)

class A {
    a = '';
    constructor(a: string) {

    }
}

class B {
    a = '';
    static test(): void {
        console.log('I do work');
    }
}

type ArgumentTypes<T> = T extends new (...a: infer A) => any? A : [] 
abstract class Base<T extends new (...a: any[])=> any> {
    Prop: InstanceType<T>;
    constructor(TCreator: T, ...a: ArgumentTypes<T>) {
        this.Prop = new TCreator(...a);
    }

    getModel(): T {
        throw new Error('Method not implemented.'); // Error because don't know how to fix it
    }
}

class RealA extends Base<typeof A> {
    getModel(): typeof A { // doesn't work - compilation error
        return A;
    }
}

class RealB extends Base<typeof B> {
    getModel(): typeof B { // works
        return B;
    }
}

var test = new RealA(A, ""); // ok
var test2 = new RealB(B)
于 2018-12-02T17:59:33.450 回答