Recently, I made an interesting discovery in JavaScript:
function foo() {
console.log("FOO");
}
foo.bar = "FOOBAR";
foo(); // logs "FOO"
console.log(foo.bar); // "FOOBAR"
This got me thinking: How would I represent this type of object/function in TypeScript, especially when passing it to another function as a parameter?
As expected, trying to access the property throws an error...
type Foo = () => void;
function test(foo: Foo) {
console.log(foo.bar); // Property 'bar' does not exist on type '() => void'.ts(2339)
}
Any suggestions besides resorting to declaring it as any
?