erratic-gate/src/common/dom/helpers/querySelector.ts
Matei Adriel fccc1922fb loading
2019-07-23 22:53:59 +03:00

15 lines
486 B
TypeScript

/**
* A type-safe querySelector function, which throws if the given element was not found
*
* @credit https://gitlab.com/wavedistrict/web-client/blob/master/src/common/dom/helpers/querySelector.ts
*/
export function querySelector<E extends Element>(
selector: string,
parent: Element = document.body
) {
const element = parent.querySelector(selector)
if (!element) {
throw `Could not find element with selector "${selector}"`
}
return element as E
}