I am seeking to create a callable function foo()
(without using the new
operator) that will also include a property foo.bar()
. The JavaScript implementation would be as follows:
function foo() {
// ...
}
foo.prototype.bar = function bar() {
// ...
}
In attempting to do this in TypeScript with an interface, I used the following approach:
interface IFoo {
(): any;
bar(): any;
}
const foo: IFoo = function(): any {
// ...
};
foo.prototype.bar = function bar(): any {
// ...
};
However, I encountered an error:
error TS2322: Type '() => any' is not assignable to type 'IFoo'.
It appears that TypeScript is having trouble during the process of defining foo
and its property bar
, as foo
does not yet have the bar
property when assigning it to a const
of type IFoo
.
How can this issue be resolved?
Put differently, how can I provide an implementation for IFoo
?