In my application, I have a method that can accept various types of functions. One specific function called constant
exists, and I need to prevent any functions with the same signature from being passed as arguments - all without changing the parameter type of the method.
To achieve this, I made an attempt using instanceof
const constant = (number) => number
class Context {
execute(fn: (...args?: any) => any) {
if (fn instanceof constant)
return false
}
}
However, it seems that the instanceof
operation does not work between functions in this case.
What methods or techniques are there for checking at runtime if a function shares the same signature as another function in TypeScript?
It's worth noting that although I'm asking for a TypeScript solution, vanilla JavaScript approaches would also be suitable if TypeScript cannot provide a more elegant solution.