There seems to be an issue with TypeScript not recognizing that a function of type Function is not callable.
type Constructable = { new(...args: any[]): any }
function isClass(func: any) {
return (
typeof func === 'function' &&
/^class\s/.test(Function.prototype.toString.call(func))
)
}
function coerceOne(data: any, fn: Function | Constructable) {
if (isClass(fn)) {
const constructor = fn as Constructable
return new constructor(data)
} else {
return fn(data) // <-- ERROR
}
}
Error:
This expression is not callable.
No constituent of type 'Function | Constructable' is callable.(2349)
Any suggestions on how to troubleshoot this problem?