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
Post a Comment