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