[英]FlowType: Inheritance of Types (Type A is a subset of type B …)
自Flow 42發布以來,您可以使用對象類型傳播。
type TypeB = { ...TypeA };
🎉
我想這是關於FlowType的初學者類型的問題,但我真的找不到滿足我的答案。
我們假設我有這種類型
type A = {
x: number
}
type B = {
y: string
}
現在我想要另一種基於類型A和B的類型,如下所示:
type C = {
...A,
...B,
z: boolean
}
恕我直言應該被解釋為:
type C = {
x: number,
y: string,
z: boolean
}
但這顯然不起作用。
你能給我一些建議或最佳實踐嗎? 非常感謝。
我已經證實傳播正如你所期望的那樣,交叉點讓我到處都是。 這是一個簡短的游樂場,顯示流量類型交叉點未能使道具可選,而傳播有效 。
后代的示例代碼:
// @flow
export type Affiliation = {
id: string,
}
export type Address = {
address1: string,
}
export type BaseContact = {
id?: string,
addresses: Array<Address>,
affiliations?: Array<Affiliation>,
}
type C = BaseContact & {
addresses: Array<Address>,
}
type CC = {
...BaseContact,
addresses?: Array<Address>,
}
type CCI = BaseContact & {
addresses?: Array<Address>,
}
class X {}
const c: C = new X() // fails
const cc: CC = new X() // fails
const cci: CCI = new X() // works
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.