在TypeScript中,我可以执行以下操作:interface MyObj { a: 'foo' | 'bar'}interface MyExtObj extends Pick<MyObj, Exclude<keyof MyObj, 'a'>> { a: MyObj['a'] | 'baz'}const test: MyExtObj = { a: 'baz' // Works fine, as well as 'foo' and 'bar'}是否可以使用Flow对象类型执行此操作,如果可以,如何执行?以下示例失败.../* @flow */type MyObj = { a: 'foo' | 'bar'}type MyExtObj = MyObj & { a: $PropertyType<MyObj, 'a'> | 'baz'}const test: MyExtObj = { a: 'baz' // Cannot assign object literal blah blah ...}
2 回答

偶然的你
TA贡献1841条经验 获得超3个赞
像这样?
/* @flow */
type MyObj = {|
a: 'foo' | 'bar'
|}
type MyExtObj = {|
...MyObj,
a: $PropertyType<MyObj, 'a'> | 'baz'
|}
const test: MyExtObj = {
a: 'baz' // Cannot assign object literal blah blah ...
}

阿波罗的战车
TA贡献1862条经验 获得超6个赞
如果由于某些原因您不想精确设置MyObj,这也可以使用:
/* @flow */
type MyObj = {
a: 'foo' | 'bar'
}
type MyExtObj = {|
...$Exact<MyObj>,
a: $PropertyType<MyObj, 'a'> | 'baz'
|}
const test: MyExtObj = {
a: 'baz' // Cannot assign object literal blah blah ...
}
添加回答
举报
0/150
提交
取消