In TypeScript I can define generic and force that in genericForObj passing only properties of a particular object.
class MyClass
{
  one(){};
  two(){};
}
function genericForObj<T, K extends keyof T>(obj: T, prop: K)
{
}
const obj = new MyClass;
genericForObj(obj, 'one'); // OK
genericForObj(MyClass, 'one');
// Error: Argument of type '"one"' is not
// assignable to parameter of type '"prototype"'.
How to tell keyof MyClass.prototype in generic function?
Thanks to @gcnew, the following genericForClass does exactly what I wanted:
class MyClass
{
  one(c: boolean){};
  two(){};
}
function genericForClass
<T extends Function, K extends keyof T['prototype']>
(classHandler: T, method: K)
{
}
genericForClass(MyClass, 'one') // OK
genericForClass(MyClass, 'fake') // Error
                        the code below returns all properties and methods of a class:
keyof typeof MyClass['prototype']
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With