summaryrefslogtreecommitdiff
path: root/client/simple/src/js/util/getElement.ts
blob: cfb2caf4e1e4febed5905d25107885ba49d721b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// SPDX-License-Identifier: AGPL-3.0-or-later

import { assertElement } from "./assertElement.ts";

type Options = {
  assert?: boolean;
};

export function getElement<T>(id: string, options?: { assert: true }): T;
export function getElement<T>(id: string, options?: { assert: false }): T | null;
export function getElement<T>(id: string, options: Options = {}): T | null {
  options.assert ??= true;

  const element = document.getElementById(id) as T | null;

  if (options.assert) {
    assertElement(element);
  }

  return element;
}