Skip to content

fix: improve NodeListOf<T>#item return type to include null #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
Loading