我正在尝试获取打字稿类和函数的参数。
export type CtorArgs<TBase> = TBase extends new (...args: infer TArgs) => infer Impl ? TArgs : never;
export type AbsCtorArgs<TBase> = TBase extends {constructor: (...args: infer TArgs) => infer Impl} ? TArgs : never;
export type FuncArgs<TBase> = TBase extends (...args: infer TArgs) => infer Impl ? TArgs : never;
function RegularFunction(a:string,b:number){}
class RegularClass{
constructor(a:string,b:number){}
}
abstract class AbstractClass{
constructor(a:string,b:number){}
}
type thing = [
CtorArgs<typeof RegularClass>,
FuncArgs<typeof RegularFunction>,
AbsCtorArgs<typeof AbstractClass>,
CtorArgs<typeof AbstractClass>,
FuncArgs<typeof AbstractClass>
];
但是由于某种原因,抽象类不返回其构造函数的参数。
我怀疑这是因为
new
关键字isn't available on abstract classes。有人知道如何获取抽象类构造函数中确实存在的那些参数吗?
不,这不是要实例化一个抽象类,而是要对从抽象类和该类的参数继承的类进行依赖注入。 See here