即使检查后,流程也会引发类型边界错误

问题描述

这里是一个示例:flow.org/try

我有两种类型:AttachmentType(具有严格的属性)和InputAttachmentType(可能什么都可以)。我需要绑定这两种类型。为此,我检查了这两个属性...但是Flow引发错误。如何解决

type AttachmentType = {|
    type: number,hash: string
|}

type InputAttachmentType = {|
    type?: ?number,hash?: ?string
|}

const handle = (attachment: InputAttachmentType) => {
    if (attachment &&
        typeof attachment.type !== 'number' &&
        typeof attachment.hash !== 'string'
    ) {
        console.log('Oops,something is wrong')
        return
    }

    add(attachment)
}

const add = (attachment: ?AttachmentType) => {
    console.log(attachment)
}

UPD:这是一个简化的示例:flow.org/try

type TypeA = {
    text: ?string
}

type TypeB = {
    text: string
}

const handle = (message: TypeA) => {
    if (typeof message.text === 'string') {
        add(message)
    }
}

const add = (message: TypeB) => {
    console.log(message)
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)