I created a function returning a selection for each selector in an object
export default function multipleSelect<T extends string>(
parent: ParentNode,
selectors: Record<T, string>
) {
const selected = {} as {
[key in keyof typeof selectors]: Element
}
for (const [key, selector] of Object.entries(selectors) as [T, string][]) {
const element = parent.querySelector(selector)
if (!element) throw new Error(`parent doesn't contain '${key}' element`)
selected[key] = element
}
return selected
}
usage:
const app = document.querySelector('.app')!
const elements = multipleSelect(app, {
title: 'h3',
list: 'ul',
canvas: 'canvas',
circle: 'circle',
currentLi: 'ul>li.current',
})
Actually, the elements type is:
const element: {
title: Element;
list: Element;
canvas: Element;
circle: Element;
currentLi: Element;
}
I would like to keep querySelector typing such that in my example the elements type is:
const element: {
title: HTMLHeadingElement;
list: HTMLUListElement;
canvas: HTMLCanvasElement;
circle: SVGCircleElement;
currentLi: Element;
}
foo=>currentLiright?