Skip to content
Open
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
37 changes: 34 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 HTMLElementTagNameMap & keyof ElementMatchesNarrowingMap<HTMLElementTagNameMap, this>>(selectors: K): this is Extract<HTMLElementTagNameMap[K], this>;
matches<K extends keyof SVGElementTagNameMap & keyof ElementMatchesNarrowingMap<SVGElementTagNameMap, this>>(selectors: K): this is Extract<SVGElementTagNameMap[K], this>;
matches<K extends keyof MathMLElementTagNameMap & keyof ElementMatchesNarrowingMap<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,34 @@ interface MathMLElementTagNameMap {
"semantics": MathMLElement;
}

type ElementMatchesNarrowingMap<ElementMap, ElementType> =
ElementType extends Element
? Element extends ElementType
? ElementMap
: [ElementMatchesBase<ElementType>] extends [never]
? {}
: ElementMatchesStrictMap<ElementMap, ElementMatchesBase<ElementType>>
: {};

type ElementMatchesBase<ElementType> =
HTMLElement extends ElementType
? HTMLElement
: SVGElement extends ElementType
? SVGElement
: MathMLElement extends ElementType
? MathMLElement
: never;

type ElementMatchesStrictMap<ElementMap, BaseElement> = {
[Key in keyof ElementMap as (
ElementMap[Key] extends BaseElement
? BaseElement extends ElementMap[Key]
? never
: Key
: never
)]: ElementMap[Key];
};

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

Expand Down
37 changes: 34 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 HTMLElementTagNameMap & keyof ElementMatchesNarrowingMap<HTMLElementTagNameMap, this>>(selectors: K): this is Extract<HTMLElementTagNameMap[K], this>;
matches<K extends keyof SVGElementTagNameMap & keyof ElementMatchesNarrowingMap<SVGElementTagNameMap, this>>(selectors: K): this is Extract<SVGElementTagNameMap[K], this>;
matches<K extends keyof MathMLElementTagNameMap & keyof ElementMatchesNarrowingMap<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,34 @@ interface MathMLElementTagNameMap {
"semantics": MathMLElement;
}

type ElementMatchesNarrowingMap<ElementMap, ElementType> =
ElementType extends Element
? Element extends ElementType
? ElementMap
: [ElementMatchesBase<ElementType>] extends [never]
? {}
: ElementMatchesStrictMap<ElementMap, ElementMatchesBase<ElementType>>
: {};

type ElementMatchesBase<ElementType> =
HTMLElement extends ElementType
? HTMLElement
: SVGElement extends ElementType
? SVGElement
: MathMLElement extends ElementType
? MathMLElement
: never;

type ElementMatchesStrictMap<ElementMap, BaseElement> = {
[Key in keyof ElementMap as (
ElementMap[Key] extends BaseElement
? BaseElement extends ElementMap[Key]
? never
: Key
: never
)]: ElementMap[Key];
};

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

Expand Down
37 changes: 34 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 HTMLElementTagNameMap & keyof ElementMatchesNarrowingMap<HTMLElementTagNameMap, this>>(selectors: K): this is Extract<HTMLElementTagNameMap[K], this>;
matches<K extends keyof SVGElementTagNameMap & keyof ElementMatchesNarrowingMap<SVGElementTagNameMap, this>>(selectors: K): this is Extract<SVGElementTagNameMap[K], this>;
matches<K extends keyof MathMLElementTagNameMap & keyof ElementMatchesNarrowingMap<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,34 @@ interface MathMLElementTagNameMap {
"semantics": MathMLElement;
}

type ElementMatchesNarrowingMap<ElementMap, ElementType> =
ElementType extends Element
? Element extends ElementType
? ElementMap
: [ElementMatchesBase<ElementType>] extends [never]
? {}
: ElementMatchesStrictMap<ElementMap, ElementMatchesBase<ElementType>>
: {};

type ElementMatchesBase<ElementType> =
HTMLElement extends ElementType
? HTMLElement
: SVGElement extends ElementType
? SVGElement
: MathMLElement extends ElementType
? MathMLElement
: never;

type ElementMatchesStrictMap<ElementMap, BaseElement> = {
[Key in keyof ElementMap as (
ElementMap[Key] extends BaseElement
? BaseElement extends ElementMap[Key]
? never
: Key
: never
)]: ElementMap[Key];
};

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

Expand Down
37 changes: 34 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 HTMLElementTagNameMap & keyof ElementMatchesNarrowingMap<HTMLElementTagNameMap, this>>(selectors: K): this is Extract<HTMLElementTagNameMap[K], this>;
matches<K extends keyof SVGElementTagNameMap & keyof ElementMatchesNarrowingMap<SVGElementTagNameMap, this>>(selectors: K): this is Extract<SVGElementTagNameMap[K], this>;
matches<K extends keyof MathMLElementTagNameMap & keyof ElementMatchesNarrowingMap<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,34 @@ interface MathMLElementTagNameMap {
"semantics": MathMLElement;
}

type ElementMatchesNarrowingMap<ElementMap, ElementType> =
ElementType extends Element
? Element extends ElementType
? ElementMap
: [ElementMatchesBase<ElementType>] extends [never]
? {}
: ElementMatchesStrictMap<ElementMap, ElementMatchesBase<ElementType>>
: {};

type ElementMatchesBase<ElementType> =
HTMLElement extends ElementType
? HTMLElement
: SVGElement extends ElementType
? SVGElement
: MathMLElement extends ElementType
? MathMLElement
: never;

type ElementMatchesStrictMap<ElementMap, BaseElement> = {
[Key in keyof ElementMap as (
ElementMap[Key] extends BaseElement
? BaseElement extends ElementMap[Key]
? never
: Key
: never
)]: ElementMap[Key];
};

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

Expand Down
69 changes: 68 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 ${mapName} & keyof ElementMatchesNarrowingMap<${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,67 @@ export function emitWebIdl(
printer.printLine("");
}

function emitElementMatchesMap() {
printer.printLine(
"type ElementMatchesNarrowingMap<ElementMap, ElementType> =",
);
printer.increaseIndent();
printer.printLine("ElementType extends Element");
printer.increaseIndent();
printer.printLine("? Element extends ElementType");
printer.increaseIndent();
printer.printLine("? ElementMap");
printer.printLine(": [ElementMatchesBase<ElementType>] extends [never]");
printer.increaseIndent();
printer.printLine("? {}");
printer.printLine(
": ElementMatchesStrictMap<ElementMap, ElementMatchesBase<ElementType>>",
);
printer.decreaseIndent();
printer.decreaseIndent();
printer.printLine(": {};");
printer.decreaseIndent();
printer.decreaseIndent();
printer.printLine("");
printer.printLine("type ElementMatchesBase<ElementType> =");
printer.increaseIndent();
printer.printLine("HTMLElement extends ElementType");
printer.increaseIndent();
printer.printLine("? HTMLElement");
printer.printLine(": SVGElement extends ElementType");
printer.increaseIndent();
printer.printLine("? SVGElement");
printer.printLine(": MathMLElement extends ElementType");
printer.increaseIndent();
printer.printLine("? MathMLElement");
printer.printLine(": never;");
printer.decreaseIndent();
printer.decreaseIndent();
printer.decreaseIndent();
printer.decreaseIndent();
printer.printLine("");
printer.printLine(
"type ElementMatchesStrictMap<ElementMap, BaseElement> = {",
);
printer.increaseIndent();
printer.printLine("[Key in keyof ElementMap as (");
printer.increaseIndent();
printer.printLine("ElementMap[Key] extends BaseElement");
printer.increaseIndent();
printer.printLine("? BaseElement extends ElementMap[Key]");
printer.increaseIndent();
printer.printLine("? never");
printer.printLine(": Key");
printer.decreaseIndent();
printer.printLine(": never");
printer.decreaseIndent();
printer.decreaseIndent();
printer.printLine(")]: ElementMap[Key];");
printer.decreaseIndent();
printer.printLine("};");
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 +1730,7 @@ export function emitWebIdl(
"MathMLElementTagNameMap",
tagNameToEleName.mathMLResult,
);
emitElementMatchesMap();
emitDeprecatedHTMLOrSVGElementTagNameMap();
emitNamedConstructors();
}
Expand Down
33 changes: 33 additions & 0 deletions unittests/files/matches.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
declare const element: Element;
declare const htmlElement: HTMLElement;
declare const htmlDivElement: HTMLDivElement;
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 (htmlElement.matches("div")) {
const narrowed: HTMLDivElement = htmlElement;
}

if (!htmlDivElement.matches("div")) {
htmlDivElement.id;
}

if (htmlDivElement.matches("object")) {
// @ts-expect-error HTMLDivElement should not narrow to HTMLObjectElement.
const narrowed: HTMLObjectElement = htmlDivElement;
}

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