I understand that flow might want to complain on the map because it can't know which Array is being mapped on, but if I refine the type like so I think it should work but doesn't.
function doSomething(c: A[] | B[]) {
if (c.length > 0) {
const a1: A | B = c[0]
if (a1.lat) {
// Flow should know that c is B[] now
c.map(cc => {
return cc;
});
}
}
}
You should specify that you will not modify array.
function doSomething(c: $ReadOnlyArray<A|B>) {
c.map(cc => {
return cc.name;
});
}
@yury I couldn't find docs related to $ReadOnlyArray, do you know where I can read up about it?
@tanhauhau
The problem with original example is that (A|B)[] is expected, but A[] | B[] is passed instead.
@yury鈥榓 suggestion of using $ReadOnlyArray fixed my example!
Most helpful comment
You should specify that you will not modify array.
Flow Try