use type guard

This commit is contained in:
Paul Bottein 2021-01-28 21:07:17 +01:00
parent b52c546a8c
commit 608a9ad347
1 changed files with 4 additions and 5 deletions

View File

@ -8,12 +8,11 @@ export class HtmlUtils {
}
public static querySelectorOrFail<T extends HTMLElement>(selector: string): T {
const elem = document.querySelector(selector);
if (elem === null) {
throw new Error("Cannot find HTML element with selector '"+selector+"'");
const elem = document.querySelector<T>(selector);
if (HtmlUtils.isHtmlElement<T>(elem)) {
return elem;
}
// FIXME: does not check the type of the returned type
return elem as T;
throw new Error("Cannot find HTML element with selector '"+selector+"'");
}
public static removeElementByIdOrFail<T extends HTMLElement>(id: string): T {