微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

这里是一个示例: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 举报,一经查实,本站将立刻删除。