javascript - Facebook Flow Function Union Type -


i'm playing around facebook flow , wonder, why following function not type check? uses union type denoted '|'.

declare var f: ((x: any) => number) | ((x: any) => string);     function f(x) {     if(true) {         return 5;     }     else return 'hello'; } 

the checker complains:

function type incompatible union type 

i know works when annotate like:

declare var f: (x: any) => number|string; 

but why former annotation fail? frankly, haven't seen union types function types anywhere far, however, don't see theoretic reason why shouldn't allowed.

((x: any) => number) | ((x: any) => string) valid expression. means f can 1 of these 2 function signatures. eg.

f = function(x: any): number {return 0} work

f = function(x: any): string {return 'hello'} work

(x: any) => number|string means return value of same function can 1 of these types dynamically, case here.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -