Skip to content

Commit c059d14

Browse files
voxpellisaschanaz
andauthored
Selector in Element.matches() should type narrow (#2147)
Co-authored-by: saschanaz <[email protected]>
1 parent 4f1c092 commit c059d14

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

baselines/dom.generated.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11292,6 +11292,9 @@ interface Element extends Node, ARIAMixin, Animatable, ChildNode, NonDocumentTyp
1129211292
*
1129311293
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Element/matches)
1129411294
*/
11295+
matches<K extends keyof HTMLElementTagNameMap>(selectors: K): this is HTMLElementTagNameMap[K];
11296+
matches<K extends keyof SVGElementTagNameMap>(selectors: K): this is SVGElementTagNameMap[K];
11297+
matches<K extends keyof MathMLElementTagNameMap>(selectors: K): this is MathMLElementTagNameMap[K];
1129511298
matches(selectors: string): boolean;
1129611299
/**
1129711300
* The **`releasePointerCapture()`** method of the Element interface releases (stops) _pointer capture_ that was previously set for a specific (PointerEvent) _pointer_.

baselines/ts5.5/dom.generated.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11282,6 +11282,9 @@ interface Element extends Node, ARIAMixin, Animatable, ChildNode, NonDocumentTyp
1128211282
*
1128311283
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Element/matches)
1128411284
*/
11285+
matches<K extends keyof HTMLElementTagNameMap>(selectors: K): this is HTMLElementTagNameMap[K];
11286+
matches<K extends keyof SVGElementTagNameMap>(selectors: K): this is SVGElementTagNameMap[K];
11287+
matches<K extends keyof MathMLElementTagNameMap>(selectors: K): this is MathMLElementTagNameMap[K];
1128511288
matches(selectors: string): boolean;
1128611289
/**
1128711290
* The **`releasePointerCapture()`** method of the Element interface releases (stops) _pointer capture_ that was previously set for a specific (PointerEvent) _pointer_.

baselines/ts5.6/dom.generated.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11292,6 +11292,9 @@ interface Element extends Node, ARIAMixin, Animatable, ChildNode, NonDocumentTyp
1129211292
*
1129311293
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Element/matches)
1129411294
*/
11295+
matches<K extends keyof HTMLElementTagNameMap>(selectors: K): this is HTMLElementTagNameMap[K];
11296+
matches<K extends keyof SVGElementTagNameMap>(selectors: K): this is SVGElementTagNameMap[K];
11297+
matches<K extends keyof MathMLElementTagNameMap>(selectors: K): this is MathMLElementTagNameMap[K];
1129511298
matches(selectors: string): boolean;
1129611299
/**
1129711300
* The **`releasePointerCapture()`** method of the Element interface releases (stops) _pointer capture_ that was previously set for a specific (PointerEvent) _pointer_.

src/build/emitter.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,19 @@ export function emitWebIdl(
613613
}
614614
}
615615

616+
/// Emit overloads for the matches method
617+
function emitMatchesOverloads(m: Browser.Method) {
618+
if (matchParamMethodSignature(m, "matches", "boolean", "string")) {
619+
const paramName = m.signature[0].param![0].name;
620+
for (const mapName of tagNameMapNames) {
621+
printer.printLine(
622+
`matches<K extends keyof ${mapName}>(${paramName}: K): this is ${mapName}[K];`,
623+
);
624+
}
625+
printer.printLine(`matches(${paramName}: string): boolean;`);
626+
}
627+
}
628+
616629
/// Emit overloads for the querySelector method
617630
function emitQuerySelectorOverloads(m: Browser.Method) {
618631
if (
@@ -988,6 +1001,8 @@ export function emitWebIdl(
9881001
return emitCreateEventOverloads(m);
9891002
case "getElementsByTagName":
9901003
return emitGetElementsByTagNameOverloads(m);
1004+
case "matches":
1005+
return emitMatchesOverloads(m);
9911006
case "querySelector":
9921007
return emitQuerySelectorOverloads(m);
9931008
case "querySelectorAll":

0 commit comments

Comments
 (0)