The problem Flow is trying to prevent me from is very well explained on this stackoverflow question
Declaring that the lower component can take an array of a sum type enables that component to push values that fulfill one or the other, like this:
const Test = <T:{}>({a}:Props<T>) => {
a.push({ cool: true })
return (
<div>
{a.map(
(props) => props.cool ?'cool' : 'other'
)}
</div>)
}
Inside the component it is not violating the type, because it is a sum type and it matches one of the types. However, the array that I'm passing down is of one type exclusively. Because of this, flow will not allow you tu pass down such array because there is the risk that it gets "perverted" inside the component.
However, if you declare the array as readOnly, you are guaranteed that nobody will push anything to your precious array, and then you can pass it down.
type Props<T:{}> = {
a: $ReadOnlyArray<T|{ +cool: boolean }>,
}
Fixed example
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…