Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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: 8 additions & 3 deletions baselines/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14051,9 +14051,12 @@ interface Element extends Node, ARIAMixin, Animatable, ChildNode, NonDocumentTyp
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Element/matches)
*/
matches<K extends keyof HTMLElementTagNameMap>(selectors: K): this is HTMLElementTagNameMap[K];
matches<K extends keyof SVGElementTagNameMap>(selectors: K): this is SVGElementTagNameMap[K];
matches<K extends keyof MathMLElementTagNameMap>(selectors: K): this is MathMLElementTagNameMap[K];
matches<K extends keyof ElementMatchesMap<HTMLElementTagNameMap, this>>(selectors: K): this is Extract<HTMLElementTagNameMap[K], this>;
matches<K extends keyof ElementMatchesMap<SVGElementTagNameMap, this>>(selectors: K): this is Extract<SVGElementTagNameMap[K], this>;
matches<K extends keyof ElementMatchesMap<MathMLElementTagNameMap, this>>(selectors: K): this is Extract<MathMLElementTagNameMap[K], this>;
matches<K extends keyof HTMLElementTagNameMap>(selectors: K): boolean;
matches<K extends keyof SVGElementTagNameMap>(selectors: K): boolean;
matches<K extends keyof MathMLElementTagNameMap>(selectors: K): boolean;
matches(selectors: string): boolean;
/**
* The **`releasePointerCapture()`** method of the Element interface releases (stops) pointer capture that was previously set for a specific (PointerEvent) pointer.
Expand Down Expand Up @@ -43880,6 +43883,8 @@ interface MathMLElementTagNameMap {
"semantics": MathMLElement;
}

type ElementMatchesMap<T, U> = { [K in keyof T as T[K] extends U ? U extends T[K] ? never : K : never]: T[K] };

/** @deprecated Directly use HTMLElementTagNameMap or SVGElementTagNameMap as appropriate, instead. */
type ElementTagNameMap = HTMLElementTagNameMap & Pick<SVGElementTagNameMap, Exclude<keyof SVGElementTagNameMap, keyof HTMLElementTagNameMap>>;

Expand Down
11 changes: 8 additions & 3 deletions baselines/ts5.5/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14038,9 +14038,12 @@ interface Element extends Node, ARIAMixin, Animatable, ChildNode, NonDocumentTyp
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Element/matches)
*/
matches<K extends keyof HTMLElementTagNameMap>(selectors: K): this is HTMLElementTagNameMap[K];
matches<K extends keyof SVGElementTagNameMap>(selectors: K): this is SVGElementTagNameMap[K];
matches<K extends keyof MathMLElementTagNameMap>(selectors: K): this is MathMLElementTagNameMap[K];
matches<K extends keyof ElementMatchesMap<HTMLElementTagNameMap, this>>(selectors: K): this is Extract<HTMLElementTagNameMap[K], this>;
matches<K extends keyof ElementMatchesMap<SVGElementTagNameMap, this>>(selectors: K): this is Extract<SVGElementTagNameMap[K], this>;
matches<K extends keyof ElementMatchesMap<MathMLElementTagNameMap, this>>(selectors: K): this is Extract<MathMLElementTagNameMap[K], this>;
matches<K extends keyof HTMLElementTagNameMap>(selectors: K): boolean;
matches<K extends keyof SVGElementTagNameMap>(selectors: K): boolean;
matches<K extends keyof MathMLElementTagNameMap>(selectors: K): boolean;
matches(selectors: string): boolean;
/**
* The **`releasePointerCapture()`** method of the Element interface releases (stops) pointer capture that was previously set for a specific (PointerEvent) pointer.
Expand Down Expand Up @@ -43854,6 +43857,8 @@ interface MathMLElementTagNameMap {
"semantics": MathMLElement;
}

type ElementMatchesMap<T, U> = { [K in keyof T as T[K] extends U ? U extends T[K] ? never : K : never]: T[K] };

/** @deprecated Directly use HTMLElementTagNameMap or SVGElementTagNameMap as appropriate, instead. */
type ElementTagNameMap = HTMLElementTagNameMap & Pick<SVGElementTagNameMap, Exclude<keyof SVGElementTagNameMap, keyof HTMLElementTagNameMap>>;

Expand Down
11 changes: 8 additions & 3 deletions baselines/ts5.6/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14048,9 +14048,12 @@ interface Element extends Node, ARIAMixin, Animatable, ChildNode, NonDocumentTyp
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Element/matches)
*/
matches<K extends keyof HTMLElementTagNameMap>(selectors: K): this is HTMLElementTagNameMap[K];
matches<K extends keyof SVGElementTagNameMap>(selectors: K): this is SVGElementTagNameMap[K];
matches<K extends keyof MathMLElementTagNameMap>(selectors: K): this is MathMLElementTagNameMap[K];
matches<K extends keyof ElementMatchesMap<HTMLElementTagNameMap, this>>(selectors: K): this is Extract<HTMLElementTagNameMap[K], this>;
matches<K extends keyof ElementMatchesMap<SVGElementTagNameMap, this>>(selectors: K): this is Extract<SVGElementTagNameMap[K], this>;
matches<K extends keyof ElementMatchesMap<MathMLElementTagNameMap, this>>(selectors: K): this is Extract<MathMLElementTagNameMap[K], this>;
matches<K extends keyof HTMLElementTagNameMap>(selectors: K): boolean;
matches<K extends keyof SVGElementTagNameMap>(selectors: K): boolean;
matches<K extends keyof MathMLElementTagNameMap>(selectors: K): boolean;
matches(selectors: string): boolean;
/**
* The **`releasePointerCapture()`** method of the Element interface releases (stops) pointer capture that was previously set for a specific (PointerEvent) pointer.
Expand Down Expand Up @@ -43877,6 +43880,8 @@ interface MathMLElementTagNameMap {
"semantics": MathMLElement;
}

type ElementMatchesMap<T, U> = { [K in keyof T as T[K] extends U ? U extends T[K] ? never : K : never]: T[K] };

/** @deprecated Directly use HTMLElementTagNameMap or SVGElementTagNameMap as appropriate, instead. */
type ElementTagNameMap = HTMLElementTagNameMap & Pick<SVGElementTagNameMap, Exclude<keyof SVGElementTagNameMap, keyof HTMLElementTagNameMap>>;

Expand Down
11 changes: 8 additions & 3 deletions baselines/ts5.9/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14048,9 +14048,12 @@ interface Element extends Node, ARIAMixin, Animatable, ChildNode, NonDocumentTyp
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Element/matches)
*/
matches<K extends keyof HTMLElementTagNameMap>(selectors: K): this is HTMLElementTagNameMap[K];
matches<K extends keyof SVGElementTagNameMap>(selectors: K): this is SVGElementTagNameMap[K];
matches<K extends keyof MathMLElementTagNameMap>(selectors: K): this is MathMLElementTagNameMap[K];
matches<K extends keyof ElementMatchesMap<HTMLElementTagNameMap, this>>(selectors: K): this is Extract<HTMLElementTagNameMap[K], this>;
matches<K extends keyof ElementMatchesMap<SVGElementTagNameMap, this>>(selectors: K): this is Extract<SVGElementTagNameMap[K], this>;
matches<K extends keyof ElementMatchesMap<MathMLElementTagNameMap, this>>(selectors: K): this is Extract<MathMLElementTagNameMap[K], this>;
matches<K extends keyof HTMLElementTagNameMap>(selectors: K): boolean;
matches<K extends keyof SVGElementTagNameMap>(selectors: K): boolean;
matches<K extends keyof MathMLElementTagNameMap>(selectors: K): boolean;
matches(selectors: string): boolean;
/**
* The **`releasePointerCapture()`** method of the Element interface releases (stops) pointer capture that was previously set for a specific (PointerEvent) pointer.
Expand Down Expand Up @@ -43877,6 +43880,8 @@ interface MathMLElementTagNameMap {
"semantics": MathMLElement;
}

type ElementMatchesMap<T, U> = { [K in keyof T as T[K] extends U ? U extends T[K] ? never : K : never]: T[K] };

/** @deprecated Directly use HTMLElementTagNameMap or SVGElementTagNameMap as appropriate, instead. */
type ElementTagNameMap = HTMLElementTagNameMap & Pick<SVGElementTagNameMap, Exclude<keyof SVGElementTagNameMap, keyof HTMLElementTagNameMap>>;

Expand Down
15 changes: 14 additions & 1 deletion src/build/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,12 @@ export function emitWebIdl(
const paramName = m.signature[0].param![0].name;
for (const mapName of tagNameMapNames) {
printer.printLine(
`matches<K extends keyof ${mapName}>(${paramName}: K): this is ${mapName}[K];`,
`matches<K extends keyof ElementMatchesMap<${mapName}, this>>(${paramName}: K): this is Extract<${mapName}[K], this>;`,
);
}
for (const mapName of tagNameMapNames) {
printer.printLine(
`matches<K extends keyof ${mapName}>(${paramName}: K): boolean;`,
);
}
printer.printLine(`matches(${paramName}: string): boolean;`);
Expand Down Expand Up @@ -715,6 +720,13 @@ export function emitWebIdl(
printer.printLine("");
}

function emitElementMatchesMap() {
printer.printLine(
"type ElementMatchesMap<T, U> = { [K in keyof T as T[K] extends U ? U extends T[K] ? never : K : never]: T[K] };",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is hard to parse, can you use something like ElementMap and ElementType than T and U? And also with parentheses.

It also kinda works weird, keyof ElementMatchesMap<HTMLElementTagNameMap, HTMLDivElement> is somehow "object" | "col" | "colgroup" | "embed" | "hr" | "iframe" | "img" | "input" | "legend" | "table" | "tbody" | "td" | "tfoot" | "th" | "thead" | "tr" ? It should be "div", right?

@cjc0013 cjc0013 Jun 30, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that makes sense. I updated the helper to make the strict-narrowing intent explicit with clearer names and a parenthesized mapped-type condition.

I also changed the narrowing map so same-type matches and structurally unrelated matches fall back to boolean. That covers the HTMLDivElement case you pointed out, so HTMLDivElement.matches("div") stays boolean and HTMLDivElement.matches("object") no longer gets a bogus type-guard path.

I added fixture coverage for the original false-branch regression, HTMLElement narrowing to HTMLDivElement, HTMLDivElement.matches("div") staying boolean, and HTMLDivElement.matches("object") not narrowing to HTMLObjectElement.

);
printer.printLine("");
}

/// Emit overloads for the createEvent method
function emitCreateEventOverloads(m: Browser.Method) {
if (matchParamMethodSignature(m, "createEvent", "Event", "string")) {
Expand Down Expand Up @@ -1664,6 +1676,7 @@ export function emitWebIdl(
"MathMLElementTagNameMap",
tagNameToEleName.mathMLResult,
);
emitElementMatchesMap();
emitDeprecatedHTMLOrSVGElementTagNameMap();
emitNamedConstructors();
}
Expand Down
19 changes: 19 additions & 0 deletions unittests/files/matches.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
declare const element: Element;
declare const htmlElement: HTMLElement;
declare const htmlTableCellElement: HTMLTableCellElement;

if (element.matches("dt")) {
const narrowed: HTMLElement = element;
}

if (!htmlElement.matches("dt")) {
htmlElement.id;
}

if (htmlElement.matches("td")) {
const narrowed: HTMLTableCellElement = htmlElement;
}

if (!htmlTableCellElement.matches("th")) {
htmlTableCellElement.id;
}
Loading