I have two different types, both in the form of arrays of objects with specified fields, combined into an intersection type in Typescript.
When I access an element from the array, I can retrieve the second field without any issues. However, when I try to use array methods such as forEach, map, or filter, the compiler throws errors.
What could be the reason behind this inconsistency between the two cases?
Below is a snippet of the sample code:
var obj: {
foo?: string;
}[] & {
bar?: boolean;
}[] = [{foo: "a", bar: true}];
obj.forEach((q) => q.bar); // bar does not exist
obj.map((q) => q.bar) // bar does not exist
obj[0].bar; // OK