In my Angular/Typescript code, I am encountering an issue with filtering a nested object array based on the object property where value === 'event'
.
Despite my efforts, the code is returning the parent object array CalendarModel[]
instead of the expected nested object array.
By adding :{types}
to the variables/filters, I can see the types that Angular recognizes.
getAllEvents: Signal<CalendarModel[]> = computed(() => this.dataService.allInformation()?.allCalendarInformation?.filter(calendar: CalendarModel => calendar?.events?.filter(event: CalendarEvent=> event.type === 'event').filter(event: CalendarEvent => {
return new Date(event.date).getTime() >= new Date().setHours(0, 0, 0, 0);
}))
);
Here is the model structure:
allCalendarInformation: CalendarModel[];
export interface CalendarModel {
date: Date;
events: CalendarEvent[];
}
export interface CalendarEvent {
imageUrl?: string
title: string;
date?: Date;
location?: string;
time?: string;
description: string;
type?: string;
featured?: boolean;
}
Am I missing something in my code logic?
Despite drilling down into events and attempting to retrieve events with type === 'event'
, Typescript is indicating that the overall object from getAllEvents
is of type CalendarModel[]
instead of CalendarEvent[]
. Why is this happening?
I have successfully implemented other filtered objects in my project, but there seems to be a mistake in this particular scenario.