允许函数在 Flow 中接受数组或字符串

Allowing a function to accept an array or a string in Flow

我有一个可以接受数组或字符串的函数:

/* @flow */
type Product = Array<string> | string

function printProducts(product: Product) {
    if (product.constructor === 'array') {
        product.map(p => console.log(p))
    } else {
        console.log(product)
    }
}

流量抱怨"property Map not found in String"。我怎样才能改变我的类型定义来满足这个要求?

使用支持的dynamic type tests, in this case Array.isArray之一:

/* @flow */
type Product = Array<string> | string

function printProducts(product: Product) {
    if (Array.isArray(product)) {
        product.map(p => console.log(p))
    } else {
        console.log(product)
    }
}