【发布时间】:2017-08-15 01:15:48
【问题描述】:
心流问题似乎很少得到答案,但这里是:
type Base = {
foo: string,
bar: string
}
type Derived1 = Base & {
conditional: false
}
type Derived2 = Base & {
conditional: true,
baz: string
}
type One = {
foo: string,
bar: string,
conditional: boolean
}
type Two = One & {
baz: string
}
type Return1 = Derived1 | Derived2 // fails
type Return2 = One | Two // works, but not desired
function test(conditional: boolean): Return1 {
return {
foo: "foo",
bar: "bar",
conditional,
...conditional ? {baz: "baz"} : {}
}
}
test 的返回值最好是Derived* 类型之一(Return1 而不是Return2),其中conditional 属性是布尔文字。
目的是让流理解如果conditional 是true,那么对象test 返回必须包含baz,反之亦然。
这不可能吗?
【问题讨论】:
标签: javascript flowtype