I have a question about defining the dynamic object value type in flow. Let me take an example:
Let's say I have an array with multiple object:
const data = [
{
type: 'dog',
value: {
bark: 'bark'
}
},
{
type: 'cat',
value: {
meow: 'meow'
}
}
]
So the idea is each object has a type and different types will have different value type. And what I am trying to do is to define the flow type for each object. It would be:
type DogValueType = {
bark: string
}
type CatValueType = {
meow: string
}
type DataType = {
type: string,
value: DogValueType | CatValueType
}
const data: Array<DataType> = [
{
type: 'dog',
value: {
bark: 'bark'
}
},
{
type: 'cat',
value: {
meow: 'meow'
}
}
]
In this simple case, Flow might be smart enough to opt in the right value type. However, if the value type is more complicated, Flow might not choose the right type. So I am wondering is it possible to define the conditional type? What I am thinking is something like(the syntax is definitely wrong, I am just trying to explain my thought):
type DataType = {
type: string,
value: type === 'dog' ? DogValueType : CatValueType
}
If this is impossible, do you think that I shouldn't mix the different value type in this case or what's the better structure to do the trick?
Any suggestion is very welcome. Thanks!