Hi, I'm writing a definition file for a library. The libraries defines a sort of DSL like:
const get = () => 42;
const obj = () => ({ get: get });
obj.get = get;
which allows the following syntax
obj().get() // 42
obj.get() // 42
Now, I'm trying to model this with flow using a union type
type Resource = {
get: () => number;
};
declare module 'foo' {
declare class Foo {
f: Resource | () => Resource;
}
declare var exports: Class<Foo>;
}
But clearly it doesn't typecheck
// @flow
import Foo from 'foo';
const f = new Foo().f;
const x: number = f().get();
const y: number = f.get();
foo.js:6
6: const x: number = f().get();
^^^ function call. Callable signature not found in
6: const x: number = f().get();
^ Resource
foo.js:6
6: const x: number = f().get();
^^^ property `get`. Property not found in
6: const x: number = f().get();
^^^ Resource
foo.js:7
7: const y: string = f.get();
^^^ property `get`. Property not found in
7: const y: string = f.get();
^ Resource
So the question is: how can I express the type of a "callable object"? It's essentially a function with some well-specified properties on it.
there are some docs here, but I don't think they're very clear: https://flowtype.org/docs/quick-reference.html#callable-objects
here's a potentially better example:
type Foo = {
(): Foo,
get: () => number
}
function bar(x: Foo) {
(x().get(): number); // ok
(x.get(): number); // ok
(x.foo(): number); // error
}
Face meets palm...
I totally missed that documentation page, thank you @mroch!
https://flowtype.org/docs/quick-reference.html#callable-objects now redirects to a place which is irrelevant. Can someone point out the current correct location for that link please?
It looks like the new version of the docs doesn't have callable objects mentioned anywhere.
https://flow.org/en/docs/types/functions/#toc-callable-objects
Most helpful comment
https://flowtype.org/docs/quick-reference.html#callable-objects now redirects to a place which is irrelevant. Can someone point out the current correct location for that link please?