Flow:从导入的对象类型中选择一个键

时间:2018-01-25 09:51:02

标签: javascript flowtype

我想从外部模块导入随机对象并使用它们的流类型:

export type randomType = {
    a: string,
    b: boolean
}

我想将randomType导入到我的库中,但我想使b可选

import { randomType } from 'randomModule';
// pseudo flow code:
type shrinkedRandomType = randomType - {b: boolean}
// How can I make this in flow so it equals this?
type shrinkedRandomType = {
    a: string,
    b?: boolean
}
// or even 
type shrinkedRandomType = {
    a: string
}

1 个答案:

答案 0 :(得分:0)

传播操作员拯救!

如果您不熟悉点差运算符look it up on MDN

否则,这样做的方法如下:

type randomType = {
    a: string,
    b: boolean
}

type shrinkedRandomType = {
    ...randomType,
    b?: boolean
}

// Perfectly valid
let test:shrinkedRandomType = {
 a: "Hi"
}