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

时间:2020-08-10 18:14:18

标签: flowtype

这里是一个示例: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)
}

0 个答案:

没有答案