Skip to content

Commit

Permalink
fix: improve NodeListOf<T>#item return type to include null (#44)
Browse files Browse the repository at this point in the history
* test: add failing test

* fix: improve NodeListOf#item return type
  • Loading branch information
uhyo committed Jul 15, 2024
1 parent 4348e7b commit 7bfcc5b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
11 changes: 11 additions & 0 deletions docs/diff/dom.generated.d.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ Index: dom.generated.d.ts
}

declare var MIDIOutputMap: {
@@ -19085,9 +19107,9 @@
new (): NodeList;
};

interface NodeListOf<TNode extends Node> 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 @@
};

Expand Down
3 changes: 2 additions & 1 deletion generated/lib.dom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19118,7 +19118,7 @@ declare var NodeList: {
};

interface NodeListOf<TNode extends Node> 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.
Expand All @@ -19130,6 +19130,7 @@ interface NodeListOf<TNode extends Node> extends NodeList {
): void;
[index: number]: TNode;
}
// item(index: number): TNode;

interface NonDocumentTypeChildNode {
/**
Expand Down
4 changes: 4 additions & 0 deletions lib/lib.dom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,7 @@ declare function structuredClone<
value: T,
options?: StructuredSerializeOptions,
): BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput<T>;

interface NodeListOf<TNode extends Node> extends NodeList {
item(index: number): TNode | null;
}
11 changes: 11 additions & 0 deletions tests/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,14 @@ const test = async (url: string) => {
}
}
}

// NodeListOf
{
// https://github.com/uhyo/better-typescript-lib/issues/43
const list: NodeListOf<HTMLDivElement> = document.querySelectorAll("div");

const item = list.item(100);
expectType<HTMLDivElement | null>(item);
// @ts-expect-error
item.append("a");
}

0 comments on commit 7bfcc5b

Please sign in to comment.