Skip to content

Commit

Permalink
Merge pull request #175 from timreichen/fix-nodelist-type
Browse files Browse the repository at this point in the history
fix: fix NodeList type inference
  • Loading branch information
b-fuze authored Aug 15, 2024
2 parents 4658549 + cf88528 commit da5fb7e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/dom/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,17 @@ export class Document extends Node {
return this._nwapi.first(selectors, this) as T;
}

querySelectorAll<T = Element>(selectors: string): NodeList<T> {
querySelectorAll<T extends Element = Element>(
selectors: string,
): NodeList<T> {
const nodeList = new NodeList();
const mutator = nodeList[nodeListMutatorSym]();

for (const match of this._nwapi.select(selectors, this)) {
mutator.push(match);
}

return nodeList;
return nodeList as NodeList<T>;
}

// TODO: DRY!!!
Expand Down
6 changes: 4 additions & 2 deletions src/dom/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,9 @@ export class Element extends Node {
return this.ownerDocument!._nwapi.first(selectors, this) as T;
}

querySelectorAll<T = Element>(selectors: string): NodeList<T> {
querySelectorAll<T extends Element = Element>(
selectors: string,
): NodeList<T> {
if (!this.ownerDocument) {
throw new Error("Element must have an owner document");
}
Expand All @@ -916,7 +918,7 @@ export class Element extends Node {
mutator.push(match);
}

return nodeList;
return nodeList as NodeList<T>;
}

matches(selectorString: string): boolean {
Expand Down
10 changes: 5 additions & 5 deletions src/dom/node-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,15 @@ for (
NodeListClass.prototype[instanceMethod] = undefined;
}

export interface NodeList<T = Node> {
export interface NodeList<T extends Node = Node> {
new (): NodeList;
readonly [index: number]: Node;
readonly [index: number]: T;
readonly length: number;
[Symbol.iterator](): Generator<Node>;
[Symbol.iterator](): Generator<T>;

item(index: number): Node;
item(index: number): T;
forEach(
cb: (node: Node, index: number, nodeList: Node[]) => void,
cb: (node: T, index: number, nodeList: T[]) => void,
thisArg?: NodeList | undefined,
): void;
[nodeListMutatorSym](): NodeListMutator;
Expand Down

0 comments on commit da5fb7e

Please sign in to comment.