Skip to content

'innerText' should not be nullable in most of Node's derived types #10315

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

Open
DanielRosenwasser opened this issue Aug 13, 2016 · 7 comments
Open
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Milestone

Comments

@DanielRosenwasser
Copy link
Member

Compile the following with --strictNullChecks with the DOM available.

const foo = document.createElement("div");
const s: string = foo.textContent;

Expected: No error
Actual: Type 'string | null' is not assignable to type 'string'.

From https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent:

  • textContent returns null if the element is a document, a document type, or a notation. [...]
  • If the node is a CDATA section, a comment, a processing instruction, or a text node, textContent returns the text inside this node (the nodeValue).
  • For other node types, textContent returns the concatenation of the textContent attribute value of every child node, excluding comments and processing instruction nodes. This is an empty string if the node has no children.

Proposed fix: textContent will be defined as nullable inNode, but overridden asstring` in most of its descendants.

@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Aug 13, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Aug 27, 2016

PRs welcomed. You can find more information about contributing lib.d.ts fixes at https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes.

@mhegazy mhegazy added the Help Wanted You can do this label Aug 27, 2016
@mhegazy mhegazy modified the milestone: Community Sep 21, 2016
@FranklinWhale
Copy link

Unfortunately null is always allowed when textContent is set:

https://dom.spec.whatwg.org/#dom-node-textcontent

I have tried to use getter and setter to fix this, but that seems not possible because "'get' and 'set' accessor must have the same type".

get textContent(): string;
set textContent(value: string | null);

@zanona
Copy link

zanona commented Nov 8, 2018

Another test case:

const span : HTMElement | null = document.body.querySelector('span');
let value: string = '';
if (span instanceof HTMLElement) {
   value = span.textContent; // Type 'string | null' is not assignable to type 'string'. 
}

Expected:

Valid, since anything subclass of Element type should always return a string.

@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
@ExE-Boss
Copy link
Contributor

ExE-Boss commented Dec 9, 2020

@FranklinWhale That depends on #32821/#2521.

@lionel-rowe
Copy link
Contributor

Can't TypeScript just be a little stricter than the spec here, and disallow setting textContent to null on nodes where its value is always a string? I can't fathom why you'd ever even want to do that (as opposed to setting it to "", which has functionally identical behavior)

@Jamesernator
Copy link

Can't TypeScript just be a little stricter than the spec here, and disallow setting textContent to null on nodes where its value is always a string?

It doesn't even need to, as far as I can tell the needed behaviour here has worked since TS4.3.

@Andarist
Copy link
Contributor

This would be quite a nice improvement. The DOM types are heavily generated and it's not straightforward to tinker with them. Perhaps @saschanaz could advise how this could be achieved in practice there? I could give it a shot then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

9 participants