Skip to content

Commit

Permalink
Merge pull request #885 from jun-sheaf/master
Browse files Browse the repository at this point in the history
Replaces several DOM methods with weaker typings and closes microsoft/TypeScript#4689
  • Loading branch information
Orta Therox committed Dec 12, 2020
2 parents f79423c + 39e338e commit d561e08
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 12 deletions.
18 changes: 11 additions & 7 deletions baselines/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4803,11 +4803,11 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
* Returns a reference to the first object with the specified value of the ID attribute.
* @param elementId String that specifies the ID value.
*/
getElementById(elementId: string): HTMLElement | null;
getElementById<E extends Element = HTMLElement>(elementId: string): E | null;
/**
* Returns a HTMLCollection of the elements in the object on which the method was invoked (a document or an element) that have all the classes given by classNames. The classNames argument is interpreted as a space-separated list of classes.
*/
getElementsByClassName(classNames: string): HTMLCollectionOf<Element>;
getElementsByClassName<E extends Element = HTMLElement>(classNames: string): HTMLCollectionOf<E>;
/**
* Gets a collection of objects based on the value of the NAME or ID attribute.
* @param elementName Gets a collection of objects based on the value of the NAME or ID attribute.
Expand Down Expand Up @@ -5003,7 +5003,7 @@ interface DocumentEvent {
/** A minimal document object that has no parent. It is used as a lightweight version of Document that stores a segment of a document structure comprised of nodes just like a standard document. The key difference is that because the document fragment isn't part of the active document tree structure, changes made to the fragment don't affect the document, cause reflow, or incur any performance impact that can occur when changes are made. */
interface DocumentFragment extends Node, NonElementParentNode, ParentNode {
readonly ownerDocument: Document;
getElementById(elementId: string): HTMLElement | null;
getElementById<E extends Element = HTMLElement>(elementId: string): E | null;
}

declare var DocumentFragment: {
Expand Down Expand Up @@ -5189,7 +5189,7 @@ interface Element extends Node, Animatable, ChildNode, InnerHTML, NonDocumentTyp
/**
* Returns a HTMLCollection of the elements in the object on which the method was invoked (a document or an element) that have all the classes given by classNames. The classNames argument is interpreted as a space-separated list of classes.
*/
getElementsByClassName(classNames: string): HTMLCollectionOf<Element>;
getElementsByClassName<E extends Element = HTMLElement>(classNames: string): HTMLCollectionOf<E>;
getElementsByTagName<K extends keyof HTMLElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<HTMLElementTagNameMap[K]>;
getElementsByTagName<K extends keyof SVGElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<SVGElementTagNameMap[K]>;
getElementsByTagName(qualifiedName: string): HTMLCollectionOf<Element>;
Expand Down Expand Up @@ -6640,6 +6640,7 @@ interface HTMLElement extends Element, DocumentAndElementEventHandlers, ElementC
readonly offsetParent: Element | null;
readonly offsetTop: number;
readonly offsetWidth: number;
readonly parentElement: HTMLElement | null;
spellcheck: boolean;
title: string;
translate: boolean;
Expand Down Expand Up @@ -10833,7 +10834,7 @@ interface Node extends EventTarget {
/**
* Returns the parent element.
*/
readonly parentElement: HTMLElement | null;
readonly parentElement: Element | null;
/**
* Returns the parent.
*/
Expand Down Expand Up @@ -11091,7 +11092,7 @@ interface NonElementParentNode {
/**
* Returns the first element within node's descendants whose ID is elementId.
*/
getElementById(elementId: string): Element | null;
getElementById<E extends Element = HTMLElement>(elementId: string): E | null;
}

interface NotificationEventMap {
Expand Down Expand Up @@ -13020,7 +13021,9 @@ interface SVGElement extends Element, DocumentAndElementEventHandlers, DocumentA
/** @deprecated */
readonly className: any;
readonly ownerSVGElement: SVGSVGElement | null;
readonly parentElement: SVGElement | null;
readonly viewportElement: SVGElement | null;
getElementsByClassName<E extends Element = SVGElement>(classNames: string): HTMLCollectionOf<E>;
addEventListener<K extends keyof SVGElementEventMap>(type: K, listener: (this: SVGElement, ev: SVGElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof SVGElementEventMap>(type: K, listener: (this: SVGElement, ev: SVGElementEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
Expand Down Expand Up @@ -13608,6 +13611,7 @@ interface SVGForeignObjectElement extends SVGGraphicsElement {
readonly width: SVGAnimatedLength;
readonly x: SVGAnimatedLength;
readonly y: SVGAnimatedLength;
getElementsByClassName<E extends Element = HTMLElement>(classNames: string): HTMLCollectionOf<E>;
addEventListener<K extends keyof SVGElementEventMap>(type: K, listener: (this: SVGForeignObjectElement, ev: SVGElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof SVGElementEventMap>(type: K, listener: (this: SVGForeignObjectElement, ev: SVGElementEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
Expand Down Expand Up @@ -14411,7 +14415,7 @@ interface SVGSVGElement extends SVGGraphicsElement, DocumentEvent, SVGFitToViewB
forceRedraw(): void;
getComputedStyle(elt: Element, pseudoElt?: string | null): CSSStyleDeclaration;
getCurrentTime(): number;
getElementById(elementId: string): Element;
getElementById<E extends Element = HTMLElement>(elementId: string): E | null;
getEnclosureList(rect: SVGRect, referenceElement: SVGElement | null): NodeListOf<SVGCircleElement | SVGEllipseElement | SVGImageElement | SVGLineElement | SVGPathElement | SVGPolygonElement | SVGPolylineElement | SVGRectElement | SVGTextElement | SVGUseElement>;
getIntersectionList(rect: SVGRect, referenceElement: SVGElement | null): NodeListOf<SVGCircleElement | SVGEllipseElement | SVGImageElement | SVGLineElement | SVGPathElement | SVGPolygonElement | SVGPolylineElement | SVGRectElement | SVGTextElement | SVGUseElement>;
pauseAnimations(): void;
Expand Down
4 changes: 2 additions & 2 deletions inputfiles/addedTypes.json
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@
"getElementsByClassName": {
"name": "getElementsByClassName",
"override-signatures": [
"getElementsByClassName(classNames: string): HTMLCollectionOf<Element>"
"getElementsByClassName<E extends Element = HTMLElement>(classNames: string): HTMLCollectionOf<E>"
]
},
"closest": {
Expand Down Expand Up @@ -972,7 +972,7 @@
"name": "getElementById",
"exposed": "Window",
"override-signatures": [
"getElementById(elementId: string): HTMLElement | null"
"getElementById<E extends Element = HTMLElement>(elementId: string): E | null"
]
}
}
Expand Down
78 changes: 75 additions & 3 deletions inputfiles/overridingTypes.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
{
"mixins": {
"mixin": {
"NonElementParentNode": {
"name": "NonElementParentNode",
"methods": {
"method": {
"getElementById": {
"name": "getElementById",
"override-signatures": [
"getElementById<E extends Element = HTMLElement>(elementId: string): E | null"
]
}
}
}
},
"GlobalFetch": {
"name": "GlobalFetch",
"methods": {
"method": {
"fetch": {
"name": "fetch",
"override-signatures": [
"fetch(input: RequestInfo, init?: RequestInit): Promise<Response>"
]
}
}
}
},
"Animatable": {
"name": "Animatable",
"methods": {
Expand Down Expand Up @@ -499,6 +525,18 @@
},
"override-index-signatures": []
},
"HTMLElement": {
"name": "HTMLElement",
"properties": {
"property": {
"parentElement": {
"name": "parentElement",
"read-only": 1,
"override-type": "HTMLElement | null"
}
}
}
},
"Document": {
"name": "Document",
"methods": {
Expand All @@ -524,7 +562,7 @@
"getElementById": {
"name": "getElementById",
"override-signatures": [
"getElementById(elementId: string): HTMLElement | null"
"getElementById<E extends Element = HTMLElement>(elementId: string): E | null"
]
},
"getElementsByTagNameNS": {
Expand All @@ -538,7 +576,7 @@
"getElementsByClassName": {
"name": "getElementsByClassName",
"override-signatures": [
"getElementsByClassName(classNames: string): HTMLCollectionOf<Element>"
"getElementsByClassName<E extends Element = HTMLElement>(classNames: string): HTMLCollectionOf<E>"
]
},
"getElementsByName": {
Expand Down Expand Up @@ -687,7 +725,7 @@
"override-type": "Node & ParentNode | null"
},
"parentElement": {
"override-type": "HTMLElement | null"
"override-type": "Element | null"
},
"childNodes": {
"override-type": "NodeListOf<ChildNode>"
Expand Down Expand Up @@ -898,6 +936,21 @@
"deprecated": 1,
"name": "className",
"type": "any"
},
"parentElement": {
"name": "parentElement",
"read-only": 1,
"override-type": "SVGElement | null"
}
}
},
"methods": {
"method": {
"getElementsByClassName": {
"name": "getElementsByClassName",
"override-signatures": [
"getElementsByClassName<E extends Element = SVGElement>(classNames: string): HTMLCollectionOf<E>"
]
}
}
}
Expand All @@ -906,6 +959,12 @@
"name": "SVGSVGElement",
"methods": {
"method": {
"getElementById": {
"name": "getElementById",
"override-signatures": [
"getElementById<E extends Element = HTMLElement>(elementId: string): E | null"
]
},
"getEnclosureList": {
"name": "getEnclosureList",
"override-signatures": [
Expand Down Expand Up @@ -937,6 +996,19 @@
}
}
},
"SVGForeignObjectElement": {
"name": "SVGForeignObjectElement",
"methods": {
"method": {
"getElementsByClassName": {
"name": "getElementsByClassName",
"override-signatures": [
"getElementsByClassName<E extends Element = HTMLElement>(classNames: string): HTMLCollectionOf<E>"
]
}
}
}
},
"FormData": {
"name": "FormData",
"methods": {
Expand Down

0 comments on commit d561e08

Please sign in to comment.