如何为泛型静态方法正确应用泛型类型定义?
How to correctly apply generic type definition for generic static methods?
我有以下基础和派生 类。
class GenericBase<T = any> {
static method(id: any) {
console.log(`${id}: ${this.name}#method`);
}
public someProp!: T;
}
class DerivedGeneric extends GenericBase<Date> {}
我正在寻找一种方法来正确应用允许我调用静态方法的类型定义。以下是我到目前为止尝试过的方法。
const t1: typeof GenericBase = DerivedGeneric;
t1.method("t1");
type Type<T> = new (...arg: any[]) => T;
const t2: Type<GenericBase> = DerivedGeneric;
t2.method("t2");
对于第一个 (t1
),TypeScript 显示以下错误
Type 'typeof DerivedGeneric' is not assignable to type 'typeof GenericBase'.
Type 'DerivedGeneric' is not assignable to type 'GenericBase'.
Types of property 'someProp' are incompatible.
Type 'Date' is not assignable to type 'T'.
对于第二个,它显示以下错误。
Property 'method' does not exist on type 'Type>'.
自然地,下面的工作没有任何编译时错误...
const t3: Function = DerivedGeneric;
(t3 as typeof DerivedGeneric).method("t3");
...以下也是如此,但现在出现运行时错误。
const t4: Function = () => {};
(t4 as typeof DerivedGeneric).method("t4");
没有泛型,第一种方法 (typeof *Base*
) 工作得很好。您可以从这个 playground link 中查看。显然,所有方法(t4
除外)都在运行时工作,只有编译时错误困扰着我。
有什么方法可以更正泛型的类型吗?
编辑:
Link to playground 具有以下类型。
type Type<T> = new (...arg: any[]) => T;
type func = Pick<typeof GenericBase, keyof typeof GenericBase> & Type<GenericBase>;
问题在于,由于基础 class 具有泛型类型参数,因此它的构造函数是泛型构造函数。这将是构造函数签名的样子:
const t3 : new <T>(...arg: any[]) => GenericBase<T> = GenericBase
这就是为什么当您尝试将 DerivedGeneric
分配给 typeof GenericBase
时却不能,因为 DerivedGeneric
没有这样的通用构造函数。
如果您只想要一个代表 class 静态的类型,您可以使用 Pick
从 typeof GenericBase
:
中删除通用构造函数签名
const t1: Pick<typeof GenericBase, keyof typeof GenericBase> = DerivedGeneric; // OK
t1.method("t1");
您还可以创建构造函数 returns GenericBase<any>
和静态成员的交集。
type Type<T> = new (...args: unknown[]) => T;
const t1: Type<GenericBase> & Pick<typeof GenericBase, keyof typeof GenericBase> = DerivedGeneric;
t1.method("t1");
new t1()
注意:它不能与 ...args: any[]
一起使用,any
有点特殊不知道它是如何发挥作用的,但无论如何应该首选 unknown
。
我有以下基础和派生 类。
class GenericBase<T = any> {
static method(id: any) {
console.log(`${id}: ${this.name}#method`);
}
public someProp!: T;
}
class DerivedGeneric extends GenericBase<Date> {}
我正在寻找一种方法来正确应用允许我调用静态方法的类型定义。以下是我到目前为止尝试过的方法。
const t1: typeof GenericBase = DerivedGeneric;
t1.method("t1");
type Type<T> = new (...arg: any[]) => T;
const t2: Type<GenericBase> = DerivedGeneric;
t2.method("t2");
对于第一个 (t1
),TypeScript 显示以下错误
Type 'typeof DerivedGeneric' is not assignable to type 'typeof GenericBase'. Type 'DerivedGeneric' is not assignable to type 'GenericBase'. Types of property 'someProp' are incompatible. Type 'Date' is not assignable to type 'T'.
对于第二个,它显示以下错误。
Property 'method' does not exist on type 'Type>'.
自然地,下面的工作没有任何编译时错误...
const t3: Function = DerivedGeneric;
(t3 as typeof DerivedGeneric).method("t3");
...以下也是如此,但现在出现运行时错误。
const t4: Function = () => {};
(t4 as typeof DerivedGeneric).method("t4");
没有泛型,第一种方法 (typeof *Base*
) 工作得很好。您可以从这个 playground link 中查看。显然,所有方法(t4
除外)都在运行时工作,只有编译时错误困扰着我。
有什么方法可以更正泛型的类型吗?
编辑: Link to playground 具有以下类型。
type Type<T> = new (...arg: any[]) => T;
type func = Pick<typeof GenericBase, keyof typeof GenericBase> & Type<GenericBase>;
问题在于,由于基础 class 具有泛型类型参数,因此它的构造函数是泛型构造函数。这将是构造函数签名的样子:
const t3 : new <T>(...arg: any[]) => GenericBase<T> = GenericBase
这就是为什么当您尝试将 DerivedGeneric
分配给 typeof GenericBase
时却不能,因为 DerivedGeneric
没有这样的通用构造函数。
如果您只想要一个代表 class 静态的类型,您可以使用 Pick
从 typeof GenericBase
:
const t1: Pick<typeof GenericBase, keyof typeof GenericBase> = DerivedGeneric; // OK
t1.method("t1");
您还可以创建构造函数 returns GenericBase<any>
和静态成员的交集。
type Type<T> = new (...args: unknown[]) => T;
const t1: Type<GenericBase> & Pick<typeof GenericBase, keyof typeof GenericBase> = DerivedGeneric;
t1.method("t1");
new t1()
注意:它不能与 ...args: any[]
一起使用,any
有点特殊不知道它是如何发挥作用的,但无论如何应该首选 unknown
。