Understanding an intersection of function types in flow

后端 未结 1 1341
既然无缘
既然无缘 2021-01-26 19:02

Looking for someone with an understanding of the static type checker flow to explain to me where i am taking a wrong turn here. I am trying to understand sub-typing function int

相关标签:
1条回答
  • 2021-01-26 19:23

    Intersection types in Flow seem to be "badly broken" (https://github.com/facebook/flow/issues/6482#issuecomment-421167167). I can't speak to why test6 and test7 fail but I would like to point out that the type number & string is an "impossible" type (https://flow.org/en/docs/types/intersections/#toc-impossible-intersection-types). From the documentation:

    // @flow
    type NumberAndString = number & string;
    
    function method(value: NumberAndString) {
      // ...
    }
    
    // $ExpectError
    method(3.14); // Error!
    // $ExpectError
    method('hi'); // Error!
    

    What we observe is that neither number nor string can satisfy number & string. Thus, I honestly can't figure out why test1 and test2 would satisfy the type (number & string) => number | string. I would love some clarity on how intersection types do (or should) work in Flow. For now, though, it seems like they are impractical to use because of their broken nature.

    0 讨论(0)
提交回复
热议问题