我正在尝试从联合类型列表中筛选出特定类型的对象。name: string
/* @flow */
type Human = {|
name: string
|};
type Droid = {|
model: string
|};
type LivingThings = {
things: [Human | Droid]
}
const getHumans = (livingThings: LivingThings): Human[] => {
return livingThings.things.filter((thing) => {
return 'name' in thing;
})
}
然而,根据这个链接,这抱怨一个错误。链接
return livingThings.things.things.filter((things)=> { ^ 不能返回。
livingThings.things.filter(...)
因为财产model
漏掉Human
1 但存在于Droid
[2]在arr