Exploring object Arrays in TypeScript.
const objArr1: { commonProperty: string; ABC: string }[] = [
{
commonProperty: "I am common",
ABC: "I am different in object 1",
},
{
commonProperty: "I am common",
ABC: "I am different in object 1",
},
]
const objArr2: { commonProperty: string; XYZ: string }[] = [
{
commonProperty: "I am common",
XYZ: "I am different in object 2",
},
{
commonProperty: "I am common",
XYZ: "I am different in object 2",
},
]
Attempting to access unique properties in the combined array,
[...objArr1, ...objArr2].forEach(obj => {
obj.commonProperty // No issue
obj.ABC // TypeScript raises error
obj.XYZ // Another TypeScript error
})
Encountering trouble as XYZ
does not exist on elements of objArr1
, and ABC
on elements of objArr2
.
Seeking suggestions for a workaround without resorting to type assertions or using the any
keyword.