Reputation: 27286
This is how you annotate a variable / constant as holding a function of a particular type:
declare type TFunction = () => any;
const foo: TFunction = function foo() {};
What is the syntax when one declares a function:
function boo() {}
?
Upvotes: 4
Views: 147
Reputation: 8662
There's no way to put: TFunction
on your function boo()
declaration. However, you can flow check it by writing the no-op statement (boo: TFunction);
afterward. The only drawback is this evaluates boo
at runtime.
Probably the best way to do it though is to not worry about explicitly declaring that boo
is a TFunction
, and instead just rely on Flow to check it any time you use boo
where a TFunction
is expected.
Here's a more concrete example of what I mean: (Try flow link)
/* @flow */
type ArithmeticOperation = (a: number, b: number) => number;
function add(a: number, b: number): number {
return a + b;
}
function concat(a: string, b: string): string {
return a + b;
}
function fold(array: Array<number>, operation: ArithmeticOperation): number {
return array.reduce(operation);
}
fold([1, 2, 3], add); // ok because add matches ArithmeticOperation
fold([1, 2, 3], concat); // flow error because concat doesn't match
Upvotes: 1
Reputation: 9262
Coming late to this question, but if you are talking about declarations in the sense of "compile-time declarations, separate from code, that use the declare
keyword", then per the Flow declarations docs, you should be able to declare globals like this:
declare var boo: TFunction;
or scoped items as members of their containing module or type:
declare class FunctionHolder {
boo: TFunction;
}
Upvotes: 0