Skip to content

Commit 2d6e896

Browse files
committed
Replaces several DOM methods with weaker typings and closes microsoft#4689
The following changes have been made: * All occurences of `getElementById` takes an optional type parameter for manual typing. * All occurences of `getElementsByClassName` takes an optional type parameter for manual typing. * `parentElement` now types to `Element` (See microsoft#4689)
1 parent bf671e9 commit 2d6e896

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

baselines/dom.generated.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4769,16 +4769,16 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
47694769
* Returns a reference to the first object with the specified value of the ID or NAME attribute.
47704770
* @param elementId String that specifies the ID value. Case-insensitive.
47714771
*/
4772-
getElementById(elementId: string): HTMLElement | null;
4772+
getElementById<E extends Element = Element>(elementId: string): E | null;
47734773
/**
47744774
* 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.
47754775
*/
4776-
getElementsByClassName(classNames: string): HTMLCollectionOf<Element>;
4776+
getElementsByClassName<E extends Element = Element>(classNames: string): HTMLCollectionOf<E>;
47774777
/**
47784778
* Gets a collection of objects based on the value of the NAME or ID attribute.
47794779
* @param elementName Gets a collection of objects based on the value of the NAME or ID attribute.
47804780
*/
4781-
getElementsByName(elementName: string): NodeListOf<HTMLElement>;
4781+
getElementsByName<E extends Element = Element>(elementName: string): NodeListOf<E>;
47824782
/**
47834783
* Retrieves a collection of objects based on the specified element name.
47844784
* @param name Specifies the name of an element.
@@ -4968,7 +4968,7 @@ interface DocumentEvent {
49684968
/** 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. */
49694969
interface DocumentFragment extends Node, NonElementParentNode, ParentNode {
49704970
readonly ownerDocument: Document;
4971-
getElementById(elementId: string): HTMLElement | null;
4971+
getElementById<E extends Element = Element>(elementId: string): E | null;
49724972
}
49734973

49744974
declare var DocumentFragment: {
@@ -5155,7 +5155,7 @@ interface Element extends Node, Animatable, ChildNode, InnerHTML, NonDocumentTyp
51555155
/**
51565156
* 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.
51575157
*/
5158-
getElementsByClassName(classNames: string): HTMLCollectionOf<Element>;
5158+
getElementsByClassName<E extends Element = Element>(classNames: string): HTMLCollectionOf<E>;
51595159
getElementsByTagName<K extends keyof HTMLElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<HTMLElementTagNameMap[K]>;
51605160
getElementsByTagName<K extends keyof SVGElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<SVGElementTagNameMap[K]>;
51615161
getElementsByTagName(qualifiedName: string): HTMLCollectionOf<Element>;
@@ -10772,7 +10772,7 @@ interface Node extends EventTarget {
1077210772
/**
1077310773
* Returns the parent element.
1077410774
*/
10775-
readonly parentElement: HTMLElement | null;
10775+
readonly parentElement: Element | null;
1077610776
/**
1077710777
* Returns the parent.
1077810778
*/
@@ -11031,7 +11031,7 @@ interface NonElementParentNode {
1103111031
/**
1103211032
* Returns the first element within node's descendants whose ID is elementId.
1103311033
*/
11034-
getElementById(elementId: string): Element | null;
11034+
getElementById<E extends Element = Element>(elementId: string): E | null;
1103511035
}
1103611036

1103711037
interface NotificationEventMap {
@@ -14348,7 +14348,7 @@ interface SVGSVGElement extends SVGGraphicsElement, DocumentEvent, SVGFitToViewB
1434814348
forceRedraw(): void;
1434914349
getComputedStyle(elt: Element, pseudoElt?: string | null): CSSStyleDeclaration;
1435014350
getCurrentTime(): number;
14351-
getElementById(elementId: string): Element;
14351+
getElementById<E extends Element = Element>(elementId: string): E | null;
1435214352
getEnclosureList(rect: SVGRect, referenceElement: SVGElement | null): NodeListOf<SVGCircleElement | SVGEllipseElement | SVGImageElement | SVGLineElement | SVGPathElement | SVGPolygonElement | SVGPolylineElement | SVGRectElement | SVGTextElement | SVGUseElement>;
1435314353
getIntersectionList(rect: SVGRect, referenceElement: SVGElement | null): NodeListOf<SVGCircleElement | SVGEllipseElement | SVGImageElement | SVGLineElement | SVGPathElement | SVGPolygonElement | SVGPolylineElement | SVGRectElement | SVGTextElement | SVGUseElement>;
1435414354
pauseAnimations(): void;

inputfiles/addedTypes.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@
620620
"getElementsByClassName": {
621621
"name": "getElementsByClassName",
622622
"override-signatures": [
623-
"getElementsByClassName(classNames: string): HTMLCollectionOf<Element>"
623+
"getElementsByClassName<E extends Element = Element>(classNames: string): HTMLCollectionOf<E>"
624624
]
625625
},
626626
"closest": {
@@ -1022,7 +1022,7 @@
10221022
"name": "getElementById",
10231023
"exposed": "Window",
10241024
"override-signatures": [
1025-
"getElementById(elementId: string): HTMLElement | null"
1025+
"getElementById<E extends Element = Element>(elementId: string): E | null"
10261026
]
10271027
}
10281028
}

inputfiles/overridingTypes.json

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
{
22
"mixins": {
33
"mixin": {
4+
"NonElementParentNode": {
5+
"name": "NonElementParentNode",
6+
"methods": {
7+
"method": {
8+
"getElementById": {
9+
"name": "getElementById",
10+
"override-signatures": [
11+
"getElementById<E extends Element = Element>(elementId: string): E | null"
12+
]
13+
}
14+
}
15+
}
16+
},
417
"GlobalFetch": {
518
"name": "GlobalFetch",
619
"methods": {
@@ -528,7 +541,7 @@
528541
"getElementById": {
529542
"name": "getElementById",
530543
"override-signatures": [
531-
"getElementById(elementId: string): HTMLElement | null"
544+
"getElementById<E extends Element = Element>(elementId: string): E | null"
532545
]
533546
},
534547
"elementsFromPoint": {
@@ -548,13 +561,13 @@
548561
"getElementsByClassName": {
549562
"name": "getElementsByClassName",
550563
"override-signatures": [
551-
"getElementsByClassName(classNames: string): HTMLCollectionOf<Element>"
564+
"getElementsByClassName<E extends Element = Element>(classNames: string): HTMLCollectionOf<E>"
552565
]
553566
},
554567
"getElementsByName": {
555568
"name": "getElementsByName",
556569
"override-signatures": [
557-
"getElementsByName(elementName: string): NodeListOf<HTMLElement>"
570+
"getElementsByName<E extends Element = Element>(elementName: string): NodeListOf<E>"
558571
]
559572
},
560573
"createTreeWalker": {
@@ -704,7 +717,7 @@
704717
"override-type": "Node & ParentNode | null"
705718
},
706719
"parentElement": {
707-
"override-type": "HTMLElement | null"
720+
"override-type": "Element | null"
708721
},
709722
"childNodes": {
710723
"override-type": "NodeListOf<ChildNode>"
@@ -967,6 +980,12 @@
967980
"name": "SVGSVGElement",
968981
"methods": {
969982
"method": {
983+
"getElementById": {
984+
"name": "getElementById",
985+
"override-signatures": [
986+
"getElementById<E extends Element = Element>(elementId: string): E | null"
987+
]
988+
},
970989
"getEnclosureList": {
971990
"name": "getEnclosureList",
972991
"override-signatures": [

0 commit comments

Comments
 (0)