From: Tim Düsterhus Date: Mon, 4 Jan 2021 16:12:33 +0000 (+0100) Subject: Automatically infer the return type of Dom/Traverse#child(ren)?ByTag X-Git-Tag: 5.4.0_Alpha_1~485^2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=84328661b4c29b5bf6bf3178f519777263eda9f6;p=GitHub%2FWoltLab%2FWCF.git Automatically infer the return type of Dom/Traverse#child(ren)?ByTag --- diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Traverse.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Traverse.js index 0bafff9d39..069ed6f8d8 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Traverse.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Traverse.js @@ -70,9 +70,6 @@ define(["require", "exports"], function (require, exports) { return _getChildren(element, 2 /* ClassName */, className)[0] || null; } exports.childByClass = childByClass; - /** - * Examines child elements and returns the first child which equals the given tag. - */ function childByTag(element, tagName) { return _getChildren(element, 3 /* TagName */, tagName)[0] || null; } @@ -91,9 +88,6 @@ define(["require", "exports"], function (require, exports) { return _getChildren(element, 2 /* ClassName */, className); } exports.childrenByClass = childrenByClass; - /** - * Examines child elements and returns all children which equal the given tag. - */ function childrenByTag(element, tagName) { return _getChildren(element, 3 /* TagName */, tagName); } diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Dom/Traverse.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Dom/Traverse.ts index a44e689f87..8404075449 100644 --- a/wcfsetup/install/files/ts/WoltLabSuite/Core/Dom/Traverse.ts +++ b/wcfsetup/install/files/ts/WoltLabSuite/Core/Dom/Traverse.ts @@ -89,6 +89,11 @@ export function childByClass(element: Element, className: string): Element | nul /** * Examines child elements and returns the first child which equals the given tag. */ +export function childByTag>( + element: Element, + tagName: K, +): HTMLElementTagNameMap[Lowercase] | null; +export function childByTag(element: Element, tagName: string): Element | null; export function childByTag(element: Element, tagName: string): Element | null { return _getChildren(element, Type.TagName, tagName)[0] || null; } @@ -110,6 +115,11 @@ export function childrenByClass(element: Element, className: string): Element[] /** * Examines child elements and returns all children which equal the given tag. */ +export function childrenByTag>( + element: Element, + tagName: K, +): HTMLElementTagNameMap[Lowercase][]; +export function childrenByTag(element: Element, tagName: string): Element[]; export function childrenByTag(element: Element, tagName: string): Element[] { return _getChildren(element, Type.TagName, tagName); } diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/FlexibleMenu.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/FlexibleMenu.ts index ef14280463..134ba44467 100644 --- a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/FlexibleMenu.ts +++ b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/FlexibleMenu.ts @@ -48,7 +48,7 @@ export function register(containerId: string): void { return; } - const list = DomTraverse.childByTag(container, "UL") as HTMLUListElement; + const list = DomTraverse.childByTag(container, "UL"); if (list === null) { throw "Expected an
    element as child of container '" + containerId + "'."; } @@ -99,7 +99,7 @@ export function rebuild(containerId: string): void { availableWidth -= DomUtil.styleAsInt(styles, "margin-right"); const list = _itemLists.get(containerId)!; - const items = DomTraverse.childrenByTag(list, "LI") as HTMLLIElement[]; + const items = DomTraverse.childrenByTag(list, "LI"); let dropdown = _dropdowns.get(containerId); let dropdownWidth = 0; if (dropdown !== undefined) { diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/TabMenu/Simple.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/TabMenu/Simple.ts index e58de8ff39..b2eb37eeb4 100644 --- a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/TabMenu/Simple.ts +++ b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/TabMenu/Simple.ts @@ -43,7 +43,7 @@ class TabMenuSimple { return false; } - const nav = DomTraverse.childByTag(this.container, "NAV") as HTMLElement; + const nav = DomTraverse.childByTag(this.container, "NAV"); if (nav === null) { return false; } @@ -54,7 +54,7 @@ class TabMenuSimple { return false; } - DomTraverse.childrenByTag(this.container, "DIV").forEach((container: HTMLElement) => { + DomTraverse.childrenByTag(this.container, "DIV").forEach((container) => { let name = container.dataset.name; if (!name) { name = DomUtil.identify(container);