The union type allows you to use Messagelabels with the name. The problem is that it also allows Messageuntagged labels. Consider the union that you have on this line:
type MessageBody = BaseBody | LabelledBody
This means it MessageBodymight look like this:
type BaseBody = {
+payload?: any,
+data?: any
}
or
type LabelledBody = {
+labelName: string
}
, MessageBody MessageHead , :
( 1)
{
+type: MessageType,
+payload?: any,
+data?: any
}
( 2)
{
+type: MessageType,
+labelName: string
}
, Flow , Message, (), Message 1 (. ). 1, labelName, , . - : labelName payload data . :
(Try)
type MessageWithLabel = MessageHead & LabelledBody
const exFunc = (message: MessageWithLabel) => {
[{name: 'example1'}, {name: 'potato'}].find(thing => thing.name === message.labelName)
}
disjoint union, , . (, type), , .
(Try)
type MessageWithBody = {|
+type: 'base',
+payload?: any,
+data?: any
|}
type MessageWithLabel = {|
+type: 'labelled',
+labelName: string
|}
type Message = MessageWithBody | MessageWithLabel
const exFunc = (message: Message) => {
if (message.type === 'labelled') {
const labelName = message.labelName
return [{name: 'example1'}, {name: 'potato'}].find(thing => thing.name === labelName)
} else {
}
}