type VEvent = {
type: "VEVENT";
summary: string;
};
type VCalendar = {
type: "VCALENDAR";
};
const data: Record<string, VEvent | VCalendar> = (...);
array.map((id) => {
return {
id,
title: data[id].type === "VEVENT" && data[id].summary,
};
});
It is throwing an error saying
Property 'summary' does not exist on type 'VCalendar'
, despite me verifying that data[id]
is of type VEvent by checking data[id].type
is equal to "VEVENT"
Strangely though, the error disappears when I enclosed the callback with an immediately invoked function expression (iife). I cannot figure out why this behavior occurred.
array.map((id) => ((cal: VEvent | VCalendar) => {
return {
id,
title: cal.type === "VEVENT" && cal.summary,
};
})(data[id]));