Why doesn't Flow infer the type of function depending on what it returns?

I expected this code to print the check in Flow , as it is in TypeScript :

var onClick : (() => void) | (() => boolean); onClick = () => { return true; } 

Instead, I get this error:

 4: onClick = () => { return true; } ^ function. Could not decide which case to select 3: var onClick : (() => void) | (() => boolean); ^ union type 

Is there a common name for this design decision, and what is its reasoning?

Is it possible to request a flow check to infer the return type of a function from return statements?

+8
typescript flowtype
source share
1 answer

You need to either provide an explicit expression, for example:

 type FuncV = () => void; type FuncB = () => boolean; var onClick : FuncV | FuncB; onClick = (() => { return true; }: FuncB); 

Or use an existential type, for example:

 type Func<T: (void | string)> = () => T; var f: Func<*>; f = () => { return "hello"; }; var msg = f() + " world!"; // type checks against return value above 
+4
source share

All Articles