Skip to content

Make most textContent not nullable #1942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 8, 2025
Merged
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
16 changes: 16 additions & 0 deletions baselines/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2990,6 +2990,9 @@ interface Attr extends Node {
readonly specified: boolean;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Attr/value) */
value: string;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): string;
set textContent(value: string | null);
}

declare var Attr: {
Expand Down Expand Up @@ -6531,6 +6534,9 @@ interface CharacterData extends Node, ChildNode, NonDocumentTypeChildNode {
replaceData(offset: number, count: number, data: string): void;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CharacterData/substringData) */
substringData(offset: number, count: number): string;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): string;
set textContent(value: string | null);
}

declare var CharacterData: {
Expand Down Expand Up @@ -8508,6 +8514,8 @@ interface Document extends Node, DocumentOrShadowRoot, FontFaceSource, GlobalEve
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Document/writeln)
*/
writeln(...text: string[]): void;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): null;
addEventListener<K extends keyof DocumentEventMap>(type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof DocumentEventMap>(type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
Expand All @@ -8529,6 +8537,9 @@ declare var Document: {
interface DocumentFragment extends Node, NonElementParentNode, ParentNode {
readonly ownerDocument: Document;
getElementById(elementId: string): HTMLElement | null;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): string;
set textContent(value: string | null);
}

declare var DocumentFragment: {
Expand Down Expand Up @@ -8602,6 +8613,8 @@ interface DocumentType extends Node, ChildNode {
readonly publicId: string;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DocumentType/systemId) */
readonly systemId: string;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): null;
}

declare var DocumentType: {
Expand Down Expand Up @@ -9035,6 +9048,9 @@ interface Element extends Node, ARIAMixin, Animatable, ChildNode, NonDocumentTyp
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Element/matches)
*/
webkitMatchesSelector(selectors: string): boolean;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): string;
set textContent(value: string | null);
addEventListener<K extends keyof ElementEventMap>(type: K, listener: (this: Element, ev: ElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof ElementEventMap>(type: K, listener: (this: Element, ev: ElementEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
Expand Down
16 changes: 16 additions & 0 deletions baselines/ts5.5/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2990,6 +2990,9 @@ interface Attr extends Node {
readonly specified: boolean;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Attr/value) */
value: string;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): string;
set textContent(value: string | null);
}

declare var Attr: {
Expand Down Expand Up @@ -6524,6 +6527,9 @@ interface CharacterData extends Node, ChildNode, NonDocumentTypeChildNode {
replaceData(offset: number, count: number, data: string): void;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CharacterData/substringData) */
substringData(offset: number, count: number): string;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): string;
set textContent(value: string | null);
}

declare var CharacterData: {
Expand Down Expand Up @@ -8501,6 +8507,8 @@ interface Document extends Node, DocumentOrShadowRoot, FontFaceSource, GlobalEve
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Document/writeln)
*/
writeln(...text: string[]): void;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): null;
addEventListener<K extends keyof DocumentEventMap>(type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof DocumentEventMap>(type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
Expand All @@ -8522,6 +8530,9 @@ declare var Document: {
interface DocumentFragment extends Node, NonElementParentNode, ParentNode {
readonly ownerDocument: Document;
getElementById(elementId: string): HTMLElement | null;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): string;
set textContent(value: string | null);
}

declare var DocumentFragment: {
Expand Down Expand Up @@ -8595,6 +8606,8 @@ interface DocumentType extends Node, ChildNode {
readonly publicId: string;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DocumentType/systemId) */
readonly systemId: string;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): null;
}

declare var DocumentType: {
Expand Down Expand Up @@ -9026,6 +9039,9 @@ interface Element extends Node, ARIAMixin, Animatable, ChildNode, NonDocumentTyp
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Element/matches)
*/
webkitMatchesSelector(selectors: string): boolean;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): string;
set textContent(value: string | null);
addEventListener<K extends keyof ElementEventMap>(type: K, listener: (this: Element, ev: ElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof ElementEventMap>(type: K, listener: (this: Element, ev: ElementEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
Expand Down
16 changes: 16 additions & 0 deletions baselines/ts5.6/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2990,6 +2990,9 @@ interface Attr extends Node {
readonly specified: boolean;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Attr/value) */
value: string;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): string;
set textContent(value: string | null);
}

declare var Attr: {
Expand Down Expand Up @@ -6531,6 +6534,9 @@ interface CharacterData extends Node, ChildNode, NonDocumentTypeChildNode {
replaceData(offset: number, count: number, data: string): void;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CharacterData/substringData) */
substringData(offset: number, count: number): string;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): string;
set textContent(value: string | null);
}

declare var CharacterData: {
Expand Down Expand Up @@ -8508,6 +8514,8 @@ interface Document extends Node, DocumentOrShadowRoot, FontFaceSource, GlobalEve
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Document/writeln)
*/
writeln(...text: string[]): void;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): null;
addEventListener<K extends keyof DocumentEventMap>(type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof DocumentEventMap>(type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
Expand All @@ -8529,6 +8537,9 @@ declare var Document: {
interface DocumentFragment extends Node, NonElementParentNode, ParentNode {
readonly ownerDocument: Document;
getElementById(elementId: string): HTMLElement | null;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): string;
set textContent(value: string | null);
}

declare var DocumentFragment: {
Expand Down Expand Up @@ -8602,6 +8613,8 @@ interface DocumentType extends Node, ChildNode {
readonly publicId: string;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DocumentType/systemId) */
readonly systemId: string;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): null;
}

declare var DocumentType: {
Expand Down Expand Up @@ -9035,6 +9048,9 @@ interface Element extends Node, ARIAMixin, Animatable, ChildNode, NonDocumentTyp
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Element/matches)
*/
webkitMatchesSelector(selectors: string): boolean;
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) */
get textContent(): string;
set textContent(value: string | null);
addEventListener<K extends keyof ElementEventMap>(type: K, listener: (this: Element, ev: ElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof ElementEventMap>(type: K, listener: (this: Element, ev: ElementEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
Expand Down
39 changes: 39 additions & 0 deletions inputfiles/addedTypes.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,13 @@
]
}
}
},
"textContent": {
"mdnUrl": "https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent",
"overrideSignatures": [
"get textContent(): string",
"set textContent(value: string | null)"
]
}
}
},
Expand Down Expand Up @@ -1228,6 +1235,17 @@
},
"Attr": {
"name": "Attr",
"methods": {
"method": {
"textContent": {
"mdnUrl": "https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent",
"overrideSignatures": [
"get textContent(): string",
"set textContent(value: string | null)"
]
}
}
},
"properties": {
"property": {
"ownerDocument": {
Expand All @@ -1240,6 +1258,17 @@
},
"CharacterData": {
"name": "CharacterData",
"methods": {
"method": {
"textContent": {
"mdnUrl": "https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent",
"overrideSignatures": [
"get textContent(): string",
"set textContent(value: string | null)"
]
}
}
},
"properties": {
"property": {
"ownerDocument": {
Expand All @@ -1260,6 +1289,16 @@
"type": "Document"
}
}
},
"methods": {
"method": {
"textContent": {
"mdnUrl": "https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent",
"overrideSignatures": [
"get textContent(): null"
]
}
}
}
},
"ProcessingInstruction": {
Expand Down
13 changes: 13 additions & 0 deletions inputfiles/overridingTypes.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,12 @@
"overrideType": "NodeListOf<HTMLElement>"
}
}
},
"textContent": {
"mdnUrl": "https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent",
"overrideSignatures": [
"get textContent(): null"
]
}
}
},
Expand Down Expand Up @@ -897,6 +903,13 @@
"overrideSignatures": [
"getElementById(elementId: string): HTMLElement | null"
]
},
"textContent": {
"mdnUrl": "https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent",
"overrideSignatures": [
"get textContent(): string",
"set textContent(value: string | null)"
]
}
}
},
Expand Down
48 changes: 48 additions & 0 deletions unittests/files/textContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
declare const assertType: <T>() => <T1>(
_x: T1,
) => StrictEqual<T, T1> extends true
? () => void
: T1 extends T
? { error: "Left side is not assignable to right side" }
: { error: "Right side is not assignable to left side" };

type StrictEqual<X, Y> =
(<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2
? true
: false;

// string:
declare const element: Element;
assertType<string>()(element.textContent)();
element.textContent = null;

declare const characterData: CharacterData;
assertType<string>()(characterData.textContent)();
characterData.textContent = null;

declare const text: Text;
assertType<string>()(text.textContent)();
text.textContent = null;

declare const comment: Comment;
assertType<string>()(comment.textContent)();
comment.textContent = null;

declare const processingInstruction: ProcessingInstruction;
assertType<string>()(processingInstruction.textContent)();
processingInstruction.textContent = null;

declare const documentFragment: DocumentFragment;
assertType<string>()(documentFragment.textContent)();

declare const attr: Attr;
assertType<string>()(attr.textContent)();
attr.textContent = null;

// null:
assertType<null>()(document.textContent)();

declare const documentType: DocumentType;
assertType<null>()(documentType.textContent)();

export {};