I am trying to make an abstract class with method callAction
which calls methods of class dynamically.
I have tried to write this but I am getting error.
abstract export class BaseContoller {
public callAction(method: keyof typeof this, parameters: any[]) {
this[method](parameters);
}
}
Error - This expression is not callable. Type 'unknown' has no call signatures.ts(2349)
Is there another way to achive this?
Your class can have value as well as function properties together, so to make sure your property is a function type, you can use typeof x === "function"
.
That can help to check method
's type with the call signature before method execution.
class BaseContoller {
public callAction(method: keyof typeof this, parameters: any[]) {
const property = this[method]
if(typeof property === "function") {
property(parameters);
}
}
public testFunction() {}
}