From 0c467d007df5f44cbdc99e9d8bad93769a36e2f4 Mon Sep 17 00:00:00 2001 From: uhyo Date: Mon, 15 Jul 2024 22:33:30 +0900 Subject: [PATCH 1/2] test: add failing test --- tests/src/dom.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/src/dom.ts b/tests/src/dom.ts index 59f4d12..c0b9617 100644 --- a/tests/src/dom.ts +++ b/tests/src/dom.ts @@ -137,3 +137,14 @@ const test = async (url: string) => { } } } + +// NodeListOf +{ + // https://github.com/uhyo/better-typescript-lib/issues/43 + const list: NodeListOf = document.querySelectorAll("div"); + + const item = list.item(100); + expectType(item); + // @ts-expect-error + item.append("a"); +} From 61a3aad9a97aeae7362522f80942a08ba1cc498a Mon Sep 17 00:00:00 2001 From: uhyo Date: Mon, 15 Jul 2024 22:39:41 +0900 Subject: [PATCH 2/2] fix: improve NodeListOf#item return type --- docs/diff/dom.generated.d.ts.md | 11 +++++++++++ generated/lib.dom.d.ts | 3 ++- lib/lib.dom.d.ts | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/docs/diff/dom.generated.d.ts.md b/docs/diff/dom.generated.d.ts.md index cb1bb4a..27010ee 100644 --- a/docs/diff/dom.generated.d.ts.md +++ b/docs/diff/dom.generated.d.ts.md @@ -151,6 +151,17 @@ Index: dom.generated.d.ts } declare var MIDIOutputMap: { +@@ -19085,9 +19107,9 @@ + new (): NodeList; + }; + + interface NodeListOf extends NodeList { +- item(index: number): TNode; ++ item(index: number): TNode | null; + /** + * Performs the specified action for each node in an list. + * @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the list. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. @@ -21527,11 +21549,11 @@ }; diff --git a/generated/lib.dom.d.ts b/generated/lib.dom.d.ts index 75031a1..393c6e0 100644 --- a/generated/lib.dom.d.ts +++ b/generated/lib.dom.d.ts @@ -19118,7 +19118,7 @@ declare var NodeList: { }; interface NodeListOf extends NodeList { - item(index: number): TNode; + item(index: number): TNode | null; /** * Performs the specified action for each node in an list. * @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the list. @@ -19130,6 +19130,7 @@ interface NodeListOf extends NodeList { ): void; [index: number]: TNode; } +// item(index: number): TNode; interface NonDocumentTypeChildNode { /** diff --git a/lib/lib.dom.d.ts b/lib/lib.dom.d.ts index 29397eb..0c61ce1 100644 --- a/lib/lib.dom.d.ts +++ b/lib/lib.dom.d.ts @@ -218,3 +218,7 @@ declare function structuredClone< value: T, options?: StructuredSerializeOptions, ): BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput; + +interface NodeListOf extends NodeList { + item(index: number): TNode | null; +}