Skip to content

Commit d561e08

Browse files
author
Orta Therox
authored
Merge pull request microsoft#885 from jun-sheaf/master
Replaces several DOM methods with weaker typings and closes microsoft/TypeScript#4689
2 parents f79423c + 39e338e commit d561e08

File tree

3 files changed

+88
-12
lines changed

3 files changed

+88
-12
lines changed

baselines/dom.generated.d.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4803,11 +4803,11 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
48034803
* Returns a reference to the first object with the specified value of the ID attribute.
48044804
* @param elementId String that specifies the ID value.
48054805
*/
4806-
getElementById(elementId: string): HTMLElement | null;
4806+
getElementById<E extends Element = HTMLElement>(elementId: string): E | null;
48074807
/**
48084808
* 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.
48094809
*/
4810-
getElementsByClassName(classNames: string): HTMLCollectionOf<Element>;
4810+
getElementsByClassName<E extends Element = HTMLElement>(classNames: string): HTMLCollectionOf<E>;
48114811
/**
48124812
* Gets a collection of objects based on the value of the NAME or ID attribute.
48134813
* @param elementName Gets a collection of objects based on the value of the NAME or ID attribute.
@@ -5003,7 +5003,7 @@ interface DocumentEvent {
50035003
/** 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. */
50045004
interface DocumentFragment extends Node, NonElementParentNode, ParentNode {
50055005
readonly ownerDocument: Document;
5006-
getElementById(elementId: string): HTMLElement | null;
5006+
getElementById<E extends Element = HTMLElement>(elementId: string): E | null;
50075007
}
50085008

50095009
declare var DocumentFragment: {
@@ -5189,7 +5189,7 @@ interface Element extends Node, Animatable, ChildNode, InnerHTML, NonDocumentTyp
51895189
/**
51905190
* 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.
51915191
*/
5192-
getElementsByClassName(classNames: string): HTMLCollectionOf<Element>;
5192+
getElementsByClassName<E extends Element = HTMLElement>(classNames: string): HTMLCollectionOf<E>;
51935193
getElementsByTagName<K extends keyof HTMLElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<HTMLElementTagNameMap[K]>;
51945194
getElementsByTagName<K extends keyof SVGElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<SVGElementTagNameMap[K]>;
51955195
getElementsByTagName(qualifiedName: string): HTMLCollectionOf<Element>;
@@ -6640,6 +6640,7 @@ interface HTMLElement extends Element, DocumentAndElementEventHandlers, ElementC
66406640
readonly offsetParent: Element | null;
66416641
readonly offsetTop: number;
66426642
readonly offsetWidth: number;
6643+
readonly parentElement: HTMLElement | null;
66436644
spellcheck: boolean;
66446645
title: string;
66456646
translate: boolean;
@@ -10833,7 +10834,7 @@ interface Node extends EventTarget {
1083310834
/**
1083410835
* Returns the parent element.
1083510836
*/
10836-
readonly parentElement: HTMLElement | null;
10837+
readonly parentElement: Element | null;
1083710838
/**
1083810839
* Returns the parent.
1083910840
*/
@@ -11091,7 +11092,7 @@ interface NonElementParentNode {
1109111092
/**
1109211093
* Returns the first element within node's descendants whose ID is elementId.
1109311094
*/
11094-
getElementById(elementId: string): Element | null;
11095+
getElementById<E extends Element = HTMLElement>(elementId: string): E | null;
1109511096
}
1109611097

1109711098
interface NotificationEventMap {
@@ -13020,7 +13021,9 @@ interface SVGElement extends Element, DocumentAndElementEventHandlers, DocumentA
1302013021
/** @deprecated */
1302113022
readonly className: any;
1302213023
readonly ownerSVGElement: SVGSVGElement | null;
13024+
readonly parentElement: SVGElement | null;
1302313025
readonly viewportElement: SVGElement | null;
13026+
getElementsByClassName<E extends Element = SVGElement>(classNames: string): HTMLCollectionOf<E>;
1302413027
addEventListener<K extends keyof SVGElementEventMap>(type: K, listener: (this: SVGElement, ev: SVGElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
1302513028
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
1302613029
removeEventListener<K extends keyof SVGElementEventMap>(type: K, listener: (this: SVGElement, ev: SVGElementEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
@@ -13608,6 +13611,7 @@ interface SVGForeignObjectElement extends SVGGraphicsElement {
1360813611
readonly width: SVGAnimatedLength;
1360913612
readonly x: SVGAnimatedLength;
1361013613
readonly y: SVGAnimatedLength;
13614+
getElementsByClassName<E extends Element = HTMLElement>(classNames: string): HTMLCollectionOf<E>;
1361113615
addEventListener<K extends keyof SVGElementEventMap>(type: K, listener: (this: SVGForeignObjectElement, ev: SVGElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
1361213616
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
1361313617
removeEventListener<K extends keyof SVGElementEventMap>(type: K, listener: (this: SVGForeignObjectElement, ev: SVGElementEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
@@ -14411,7 +14415,7 @@ interface SVGSVGElement extends SVGGraphicsElement, DocumentEvent, SVGFitToViewB
1441114415
forceRedraw(): void;
1441214416
getComputedStyle(elt: Element, pseudoElt?: string | null): CSSStyleDeclaration;
1441314417
getCurrentTime(): number;
14414-
getElementById(elementId: string): Element;
14418+
getElementById<E extends Element = HTMLElement>(elementId: string): E | null;
1441514419
getEnclosureList(rect: SVGRect, referenceElement: SVGElement | null): NodeListOf<SVGCircleElement | SVGEllipseElement | SVGImageElement | SVGLineElement | SVGPathElement | SVGPolygonElement | SVGPolylineElement | SVGRectElement | SVGTextElement | SVGUseElement>;
1441614420
getIntersectionList(rect: SVGRect, referenceElement: SVGElement | null): NodeListOf<SVGCircleElement | SVGEllipseElement | SVGImageElement | SVGLineElement | SVGPathElement | SVGPolygonElement | SVGPolylineElement | SVGRectElement | SVGTextElement | SVGUseElement>;
1441714421
pauseAnimations(): void;

inputfiles/addedTypes.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@
610610
"getElementsByClassName": {
611611
"name": "getElementsByClassName",
612612
"override-signatures": [
613-
"getElementsByClassName(classNames: string): HTMLCollectionOf<Element>"
613+
"getElementsByClassName<E extends Element = HTMLElement>(classNames: string): HTMLCollectionOf<E>"
614614
]
615615
},
616616
"closest": {
@@ -972,7 +972,7 @@
972972
"name": "getElementById",
973973
"exposed": "Window",
974974
"override-signatures": [
975-
"getElementById(elementId: string): HTMLElement | null"
975+
"getElementById<E extends Element = HTMLElement>(elementId: string): E | null"
976976
]
977977
}
978978
}

inputfiles/overridingTypes.json

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
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 = HTMLElement>(elementId: string): E | null"
12+
]
13+
}
14+
}
15+
}
16+
},
17+
"GlobalFetch": {
18+
"name": "GlobalFetch",
19+
"methods": {
20+
"method": {
21+
"fetch": {
22+
"name": "fetch",
23+
"override-signatures": [
24+
"fetch(input: RequestInfo, init?: RequestInit): Promise<Response>"
25+
]
26+
}
27+
}
28+
}
29+
},
430
"Animatable": {
531
"name": "Animatable",
632
"methods": {
@@ -499,6 +525,18 @@
499525
},
500526
"override-index-signatures": []
501527
},
528+
"HTMLElement": {
529+
"name": "HTMLElement",
530+
"properties": {
531+
"property": {
532+
"parentElement": {
533+
"name": "parentElement",
534+
"read-only": 1,
535+
"override-type": "HTMLElement | null"
536+
}
537+
}
538+
}
539+
},
502540
"Document": {
503541
"name": "Document",
504542
"methods": {
@@ -524,7 +562,7 @@
524562
"getElementById": {
525563
"name": "getElementById",
526564
"override-signatures": [
527-
"getElementById(elementId: string): HTMLElement | null"
565+
"getElementById<E extends Element = HTMLElement>(elementId: string): E | null"
528566
]
529567
},
530568
"getElementsByTagNameNS": {
@@ -538,7 +576,7 @@
538576
"getElementsByClassName": {
539577
"name": "getElementsByClassName",
540578
"override-signatures": [
541-
"getElementsByClassName(classNames: string): HTMLCollectionOf<Element>"
579+
"getElementsByClassName<E extends Element = HTMLElement>(classNames: string): HTMLCollectionOf<E>"
542580
]
543581
},
544582
"getElementsByName": {
@@ -687,7 +725,7 @@
687725
"override-type": "Node & ParentNode | null"
688726
},
689727
"parentElement": {
690-
"override-type": "HTMLElement | null"
728+
"override-type": "Element | null"
691729
},
692730
"childNodes": {
693731
"override-type": "NodeListOf<ChildNode>"
@@ -898,6 +936,21 @@
898936
"deprecated": 1,
899937
"name": "className",
900938
"type": "any"
939+
},
940+
"parentElement": {
941+
"name": "parentElement",
942+
"read-only": 1,
943+
"override-type": "SVGElement | null"
944+
}
945+
}
946+
},
947+
"methods": {
948+
"method": {
949+
"getElementsByClassName": {
950+
"name": "getElementsByClassName",
951+
"override-signatures": [
952+
"getElementsByClassName<E extends Element = SVGElement>(classNames: string): HTMLCollectionOf<E>"
953+
]
901954
}
902955
}
903956
}
@@ -906,6 +959,12 @@
906959
"name": "SVGSVGElement",
907960
"methods": {
908961
"method": {
962+
"getElementById": {
963+
"name": "getElementById",
964+
"override-signatures": [
965+
"getElementById<E extends Element = HTMLElement>(elementId: string): E | null"
966+
]
967+
},
909968
"getEnclosureList": {
910969
"name": "getEnclosureList",
911970
"override-signatures": [
@@ -937,6 +996,19 @@
937996
}
938997
}
939998
},
999+
"SVGForeignObjectElement": {
1000+
"name": "SVGForeignObjectElement",
1001+
"methods": {
1002+
"method": {
1003+
"getElementsByClassName": {
1004+
"name": "getElementsByClassName",
1005+
"override-signatures": [
1006+
"getElementsByClassName<E extends Element = HTMLElement>(classNames: string): HTMLCollectionOf<E>"
1007+
]
1008+
}
1009+
}
1010+
}
1011+
},
9401012
"FormData": {
9411013
"name": "FormData",
9421014
"methods": {

0 commit comments

Comments
 (0)