Is it possible to create this type: callable object (function) BUT without Function.prototype
methods?
let callableObject = () => 'foo'
callableObject.bar = 'baz'
callableObject() // 'foo'
callableObject // {bar: 'baz'}
callableObject.call // error
I tried something like this with no success:
type ExcludeFunctionPrototypeMethods<T extends () => any> = {
[K in Exclude<keyof T, keyof Function>]: T[K]
}
function f<T extends () => any>(t: T):
ExcludeFunctionPrototypeMethods<T> {
return {} as any
}
f(() => {})() // the methods are excluded, but when calling,
it fails with "Cannot invoke an expression whose type lacks a call
signature. Type 'ExcludeFunctionPrototypeMethods<() => void>'
has no compatible call signatures."
So maybe also the question should sound like "how do I add call signatures to the type"
You could do the following:
Here we use
Object.setPrototype()
to set the prototype of the function object explicitly tonull
.