流类型区分子类型联合

时间:2018-03-01 12:21:03

标签: javascript flowtype

我有扩展子类型的联合示例:

type TypeA = {|
    type: "a",
    value: number,
|};

type TypeB = {|
    type: "b",
    value: Array<number>,
|};

type SuperType =
    | TypeA & {|
        color: string,
    |}
    | TypeB;

function test(val: SuperType): void {
    if (val.type === "b") {
        // Flow should probably know that val.value is an array here
        console.log(val.value.length);
    }
}

live flow.org/try link here

但是,最后,当我尝试利用Disjoint Unions with exact types时,它失败了:

20:         console.log(val.value.length);
                                 ^ Cannot get `val.value.length` because property `length` is missing in `Number` [1].
References:
3:     value: number,
              ^ [1]

我无法弄清楚Flowtype是不支持这个还是我做错了什么。这发生在Flow 0.66上。请注意,如果我删除{| color: string |}位,则此方法有效。

1 个答案:

答案 0 :(得分:0)

library(lubridate) # A small function that prints the date # of the last month in the YYYY-MM format last_month <- function(d = today()) { day(d) <- 1 month(d) <- month(d) - 1 format(d, "%Y-%m") } # lets try it last_month() #> [1] "2018-02" file <- "Fin_report.xlsx" # replace the .xlsx with -YYYY-MM.xlsx file2 <- gsub("\\.xlsx$", paste0("-", last_month(), ".xlsx"), file) file2 #> [1] "Fin_report-2018-02.xlsx" 中,使用点差运算符SuperType代替交集运算符...

&

Live demo on Try Flow