Skip to content

ChildNode.nextSibling should be another ChildNode, not just a Node #28551

Closed
@jcormont

Description

@jcormont

TypeScript Version: 3.1.6

Search Terms: ChildNode Node nextSibling

Code

// a basic loop over elements in the DOM
let element = document.body.firstChild;  // is a ChildNode | null
while (element) {
    // ... do something with element
    let sibling = element.nextSibling;  // is a Node | null -- why?
    element = sibling;  // uh-uh!
}

Expected behavior:

Surely a sibling of a child is itself also a child (of the same parent). So siblings should be assignable using the same variable.

Actual behavior:

Error:

Type 'Node | null' is not assignable to type 'ChildNode | null'.
  Type 'Node' is not assignable to type 'ChildNode'.
    Property 'after' is missing in type 'Node'. [2322]

Related Issues:

I think this is what #27453 also refers to, but fails to describe accurately so it has been closed.

In any case this is related to the 'solution' in #24633 which clearly shows the type of nextSibling as Node, not ChildNode.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions