Skip to content

Commit 7bfcc5b

Browse files
authored
fix: improve NodeListOf<T>#item return type to include null (#44)
* test: add failing test * fix: improve NodeListOf#item return type
1 parent 4348e7b commit 7bfcc5b

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

docs/diff/dom.generated.d.ts.md

+11
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ Index: dom.generated.d.ts
151151
}
152152

153153
declare var MIDIOutputMap: {
154+
@@ -19085,9 +19107,9 @@
155+
new (): NodeList;
156+
};
157+
158+
interface NodeListOf<TNode extends Node> extends NodeList {
159+
- item(index: number): TNode;
160+
+ item(index: number): TNode | null;
161+
/**
162+
* Performs the specified action for each node in an list.
163+
* @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the list.
164+
* @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.
154165
@@ -21527,11 +21549,11 @@
155166
};
156167

generated/lib.dom.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -19118,7 +19118,7 @@ declare var NodeList: {
1911819118
};
1911919119

1912019120
interface NodeListOf<TNode extends Node> extends NodeList {
19121-
item(index: number): TNode;
19121+
item(index: number): TNode | null;
1912219122
/**
1912319123
* Performs the specified action for each node in an list.
1912419124
* @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<TNode extends Node> extends NodeList {
1913019130
): void;
1913119131
[index: number]: TNode;
1913219132
}
19133+
// item(index: number): TNode;
1913319134

1913419135
interface NonDocumentTypeChildNode {
1913519136
/**

lib/lib.dom.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,7 @@ declare function structuredClone<
218218
value: T,
219219
options?: StructuredSerializeOptions,
220220
): BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput<T>;
221+
222+
interface NodeListOf<TNode extends Node> extends NodeList {
223+
item(index: number): TNode | null;
224+
}

tests/src/dom.ts

+11
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,14 @@ const test = async (url: string) => {
137137
}
138138
}
139139
}
140+
141+
// NodeListOf
142+
{
143+
// https://github.com/uhyo/better-typescript-lib/issues/43
144+
const list: NodeListOf<HTMLDivElement> = document.querySelectorAll("div");
145+
146+
const item = list.item(100);
147+
expectType<HTMLDivElement | null>(item);
148+
// @ts-expect-error
149+
item.append("a");
150+
}

0 commit comments

Comments
 (0)