diff --git a/CLAUDE.md b/CLAUDE.md index 456bdf2..ef5b666 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -21,6 +21,8 @@ This project generates alternative TypeScript standard library definitions with - **`TypeScript/`** - Git submodule containing the official TypeScript repository for source lib files + - Note: `TypeScript/lib/lib.dom.d.ts` and `TypeScript/lib/lib.es5.d.ts` are HUGE. Never try to read the whole file at once. Always grep for specific parts. + - **`tests/`** - Type-level tests using `tsd` to verify the improved type definitions work correctly ## Essential Commands diff --git a/generated/lib.dom.d.ts b/generated/lib.dom.d.ts index ac49123..9a706c3 100644 --- a/generated/lib.dom.d.ts +++ b/generated/lib.dom.d.ts @@ -19836,7 +19836,7 @@ interface Node extends EventTarget { * * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Node/parentElement) */ - readonly parentElement: HTMLElement | null; + readonly parentElement: Element | null; /** * Returns the parent. * @@ -19941,6 +19941,12 @@ interface Node extends EventTarget { readonly DOCUMENT_POSITION_CONTAINED_BY: 0x10; readonly DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC: 0x20; } +// /** +// * Returns the parent element. +// * +// * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Node/parentElement) +// */ +// readonly parentElement: HTMLElement | null; declare var Node: { prototype: Node; diff --git a/lib/lib.dom.d.ts b/lib/lib.dom.d.ts index 493ec62..2edbf50 100644 --- a/lib/lib.dom.d.ts +++ b/lib/lib.dom.d.ts @@ -255,3 +255,12 @@ interface Document interface DocumentFragment extends Node, NonElementParentNode, ParentNode { getElementById(elementId: string): Element | null; } + +interface Node extends EventTarget { + /** + * Returns the parent element. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Node/parentElement) + */ + readonly parentElement: Element | null; +} diff --git a/tests/src/dom.ts b/tests/src/dom.ts index f1aad73..f39cff5 100644 --- a/tests/src/dom.ts +++ b/tests/src/dom.ts @@ -161,3 +161,29 @@ const test = async (url: string) => { const svgElement: SVGElement = {} as SVGElement; const elementOrNull: typeof element = svgElement; } + +// Node.parentElement +{ + const div = document.createElement("div"); + const parent = div.parentElement; + expectType(parent); + + // Verify that the return type is not specifically HTMLElement + expectNotType(parent); + + // Verify that SVGElement can be assigned to parentElement (without type assertion) + const svgElement: SVGElement = {} as SVGElement; + const elementOrNull: typeof parent = svgElement; + + // Test with SVG elements + const svgElement2 = document.createElementNS("http://www.w3.org/2000/svg", "circle"); + const svgParent = svgElement2.parentElement; + expectType(svgParent); + + // Verify SVG elements work with parentElement + if (svgParent) { + expectType(svgParent); + // Should be able to call methods available on Element + svgParent.getAttribute("id"); + } +}