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