type Object1 = {
link: 'niceurl.com'
}
type Object2 = {
source: 'differenturl.com'
}
function AnotherComponent(item: Object1 | Object2) {
return (
<img src={item?.link || item?.source} />
)
}
Output:
Error:
Property 'link' does not exist on type 'Object1 | Object2'.
Property 'link' does not exist on type 'Object2'
Expected: no error as the optional chaining meets the type guard.
I think something like ("link" in item) or a similar inline type assertion might help, but considering the component's size, these options seem cumbersome.