I'm currently working on creating a custom date-picker using web components by following a tutorial. I've encountered an error while translating the JavaScript logic to Typescript, specifically within the connectedCallback() {...}
function. The error message states:
Type 'HTMLCollection | undefined' must have a '[Symbol.iterator]()' method that returns an iterator
on this line of code: const [prevBtn, calendarDateElement, nextBtn] = this.calendarDropdown?.querySelector(".calendar-header")?.children;
. Although I've researched similar issues on Stackoverflow, none of the suggested solutions seem to work for me. Can someone please help me understand what adjustments I need to make in order to resolve this error?
class DatePicker extends HTMLElement {
shadow: ShadowRoot;
calendar: Calendar;
mounted: boolean = false;
/** Elements */
calendarDropdown: Element | null = null;
calendarDateElement: HTMLHeadingElement | null | undefined = null;
constructor() {
super();
...
}
connectedCallback() {
this.mounted = true;
this.toggleButton = this.shadow.querySelector(".date-toggle");
this.calendarDropdown = this.shadow.querySelector(".calendar-dropdown");
const [prevBtn, calendarDateElement, nextBtn] = this.calendarDropdown?.querySelector(".calendar-header")?.children; // <--- This is the line complain
this.calendarDateElement = calendarDateElement;
this.toggleButton?.addEventListener("click", () => this.toggleCalendar());
prevBtn.addEventListener("click", () => this.prevMonth());
nextBtn.addEventListener("click", () => this.nextMonth());
}
}