不能$ Diff一个交叉点类型

时间:2018-04-03 03:34:57

标签: flowtype

我遇到了一个问题,交叉点类型无法与$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]

如果有办法解决这个问题,我会使用OneBoth作为组件的支柱类型,All$Diff是HOC输入的一部分。

谢谢!

https://flow.org/try/#0PTAEAEDMBsHsHcBQAXAngBwKagPIDtsBeUAb1FgIC5Q8BXAWwCNMAnUAXxQ2wBV5ZQxMsn7U6TVhy5ZQPABYtMRUqGQKlYhszac0MgEKw1g3AVAAyWf2nYAgtGgnDxy-MWZEiAMYUAzslBGIzlKABIAEQBLSEgAHntoABpZdUwAPhMyCkxqAEZkkVhqACYOIA

1 个答案:

答案 0 :(得分:1)

我觉得流处理交集类型的方式存在一些缺点,复合它们应该有效,但并不像预期的那样。

如果您可以将All定义为OneTwoThree的交集,它将按预期工作。

$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 }