流程:如何从一组联合类型中筛选出一个类型。

问题描述 投票:0回答:1

我正在尝试从联合类型列表中筛选出特定类型的对象。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

javascript flowtype
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.