I'm trying to get the arguments for typescript classes and functions.
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>
];
But for some reason abstract classes don't return arguments of their constructors.
I suspect this is because the
new
keyword isn't available on abstract classes. Does anybody know how to get those arguments which do actually exist for an abstract class constructor?
And no, this isn't about instantiating a abstract classes, this is about dependency injection to a class that inherits from an abstract class and the arguments to that class. See here