Angular typescript中静态方法中的泛型

Angular typescript中静态方法中的泛型,angular,typescript,Angular,Typescript,非常感谢您的帮助。您可以使用自定义的类类型: stats(list: ClassType){} // something like this stats(list: any){} // currently im using this, how should I restrict param only to types. export-type-Abstract=Function&{prototype:T}; 导出类型Instantiable=new(…args:any[])=>T; 导出类型类

非常感谢您的帮助。

您可以使用自定义的
类型:

stats(list: ClassType){} // something like this
stats(list: any){} // currently im using this, how should I restrict param only to types.
export-type-Abstract=Function&{prototype:T};
导出类型Instantiable=new(…args:any[])=>T;
导出类型类=抽象|可实例化;
资料来源:

并请求一个类数组作为参数

类型不能在方法本身内部使用,这根本不能在typescript中完成,因为您无法在运行时获取类型的类


请随意探索我链接的github中的代码,我们有这个确切的问题,并使用这个自定义类类型解决了它。

为什么不让它成为
(klass:typeof T,provider:Array)
?然后调用它
.register(AboutModule,[AboutService])
,并从第一个参数推断出
T
。@jonrsharpe当我使用klass:typeof T时,它的抛出错误仅指类型,但用作值。您还需要
ngModule:klass
,而不是
T
@jonrsharpe静态寄存器(klass:typeof T,provider:Array):ModuleWithProviders{return{ngModule:klass,providers:provider};}但我仍然收到错误。我在kclass:typeof T行收到这个错误
stats(list: ClassType){} // something like this
stats(list: any){} // currently im using this, how should I restrict param only to types.
export type Abstract<T = any> = Function & { prototype: T };

export type Instantiable<T = any> = new (...args: any[]) => T;

export type Class<T = any> = Abstract<T> | Instantiable<T>;