Flow工具類型(Utility Types)

工具類型(Utility Types)

Flow提供了一組實用的工具類型板祝,可用于其他類型的操作擂涛,并可用于不同的場景未荒。

  • $Keys<T>
  • $Values<T>
  • $ReadOnly<T>
  • $Exact<T>
  • $Diff<A, B>
  • $Rest<A, B>
  • $PropertyType<T>
  • $ElementType<T>
  • $ObjMap<T, F>
  • $TupleMap<T, F>
  • $Call<F>
  • Class<T>
  • $Supertype<T>
  • $Subtype<T>
  • Existential Type (*)

$Keys<T>

參考聯(lián)合類型狞甚。

// @flow
const countries = {
  US: "United States",
  IT: "Italy",
  FR: "France"
};

type Country = $Keys<typeof countries>;

const italy: Country = 'IT';
const nope: Country = 'nope'; // 'nope' is not a Country

$Values<T>

// @flow
type Props = {
  name: string,
  age: number,
};

// The following two types are equivalent:
type PropValues = string | number;
type Prop$Values = $Values<Props>;

const name: Prop$Values = 'Jon';  // OK
const age: Prop$Values = 42;  // OK
const fn: Prop$Values = () => {};  // Error! function is not part of the union type

$ReadOnly<T>

// @flow
type Props = {
  name: string,
  age: number,
  // ...
};

type ReadOnlyProps = $ReadOnly<Props>;

function render(props: ReadOnlyProps) {
  const {name, age} = props;  // OK to read
  props.age = 42;             // Error when writing
  // ...
}

$Exact<T>

參考{| key: type |}

// @flow
type ExactUser = $Exact<{name: string}>;
type ExactUserShorthand = {| name: string |};

const user2 = {name: 'John Wilkes Booth'};
// These will both be satisfied because they are equivalent
(user2: ExactUser);
(user2: ExactUserShorthand);

$Diff<A, B>

// @flow
type Props = { name: string, age: number };
type DefaultProps = { age: number };
type RequiredProps = $Diff<Props, DefaultProps>;

function setProps(props: RequiredProps) {
  // ...
}

setProps({ name: 'foo' });
setProps({ name: 'foo', age: 42, baz: false }); // you can pass extra props too
setProps({ age: 42 }); // error, name is required

請注意等曼,如果要從中刪除屬性的對象不具有要刪除的屬性,則$Diff <A损离,B>將會報錯哥艇,即如果B具有A中不存在的鍵:

// @flow
type Props = { name: string, age: number };
type DefaultProps = { age: number, other: string }; // Will error due to this `other` property not being in Props.
type RequiredProps = $Diff<Props, DefaultProps>;

function setProps(props: RequiredProps) {
  props.name;
  // ...
}

解決方案:

type A = $Diff<{}, {nope: number}>; // Error
type B = $Diff<{}, {nope: number | void}>; // OK
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市僻澎,隨后出現(xiàn)的幾起案子貌踏,更是在濱河造成了極大的恐慌,老刑警劉巖窟勃,帶你破解...
    沈念sama閱讀 222,627評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件祖乳,死亡現(xiàn)場離奇詭異,居然都是意外死亡拳恋,警方通過查閱死者的電腦和手機凡资,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評論 3 399
  • 文/潘曉璐 我一進店門砸捏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谬运,“玉大人隙赁,你說我怎么就攤上這事“鹋” “怎么了伞访?”我有些...
    開封第一講書人閱讀 169,346評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長轰驳。 經(jīng)常有香客問我厚掷,道長,這世上最難降的妖魔是什么级解? 我笑而不...
    開封第一講書人閱讀 60,097評論 1 300
  • 正文 為了忘掉前任冒黑,我火速辦了婚禮,結(jié)果婚禮上勤哗,老公的妹妹穿的比我還像新娘抡爹。我一直安慰自己,他們只是感情好芒划,可當(dāng)我...
    茶點故事閱讀 69,100評論 6 398
  • 文/花漫 我一把揭開白布冬竟。 她就那樣靜靜地躺著,像睡著了一般民逼。 火紅的嫁衣襯著肌膚如雪泵殴。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,696評論 1 312
  • 那天拼苍,我揣著相機與錄音笑诅,去河邊找鬼。 笑死映屋,一個胖子當(dāng)著我的面吹牛苟鸯,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播棚点,決...
    沈念sama閱讀 41,165評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼早处,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了瘫析?” 一聲冷哼從身側(cè)響起砌梆,我...
    開封第一講書人閱讀 40,108評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎贬循,沒想到半個月后咸包,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,646評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡杖虾,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,709評論 3 342
  • 正文 我和宋清朗相戀三年烂瘫,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,861評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡坟比,死狀恐怖芦鳍,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情葛账,我是刑警寧澤柠衅,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站籍琳,受9級特大地震影響菲宴,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜趋急,卻給世界環(huán)境...
    茶點故事閱讀 42,196評論 3 336
  • 文/蒙蒙 一喝峦、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧呜达,春花似錦愈犹、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至嗦嗡,卻和暖如春勋锤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背侥祭。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評論 1 274
  • 我被黑心中介騙來泰國打工叁执, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人矮冬。 一個月前我還...
    沈念sama閱讀 49,287評論 3 379
  • 正文 我出身青樓谈宛,卻偏偏與公主長得像,于是被迫代替她去往敵國和親胎署。 傳聞我的和親對象是個殘疾皇子吆录,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,860評論 2 361

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)琼牧,斷路器恢筝,智...
    卡卡羅2017閱讀 134,715評論 18 139
  • Lua 5.1 參考手冊 by Roberto Ierusalimschy, Luiz Henrique de F...
    蘇黎九歌閱讀 13,836評論 0 38
  • 對象的創(chuàng)建與銷毀 Item 1: 使用static工廠方法,而不是構(gòu)造函數(shù)創(chuàng)建對象:僅僅是創(chuàng)建對象的方法巨坊,并非Fa...
    孫小磊閱讀 1,996評論 0 3
  • <extension>123</extension><mime-type>application/vnd.lotu...
    張不二01閱讀 1,003評論 0 0
  • 《有妹紙兮》第一期 姓名:金美希 地址:廣東撬槽,廣州 生日:1998.06.01
    欣茹Miss閱讀 2,194評論 0 0