我遇到了一个问题,交叉点类型无法与$Diff
一起使用。我试图为具有交叉道具的组件实现反应HOC时遇到了这个问题。
简化示例如下:
type One = { one: number }
type Two = { two: number }
type Three = { three: number }
type Both = One & Two
type All = Both & Three
const both:$Diff<All, Three> = { one: 1, two: 2 }
^ Cannot instantiate `$Diff` because undefined property `three` [1] is incompatible with number [2].
References:
6: type Both = One & Two
^ [1]
5: type Three = { three: number }
^ [2]
如果有办法解决这个问题,我会使用One
和Both
作为组件的支柱类型,All
和$Diff
是HOC输入的一部分。
谢谢!
答案 0 :(得分:1)
我觉得流处理交集类型的方式存在一些缺点,复合它们应该有效,但并不像预期的那样。
如果您可以将All
定义为One
,Two
和Three
的交集,它将按预期工作。
$Shape<>
有时可以解决这类问题但我无法在这种情况下使用它。
// @flow
type One = { one: number }
type Two = { two: number }
type Three = { three: number }
type Both = One & Two
type All = One & Two & Three
const both:$Diff<All, Three> = { one: 1, two: 2 }