question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cloneNode should return sub type, not Node

See original GitHub issue

When I do someElement.cloneNode(), I want the returned element to be the same type as someElement, but right now it’s returning type Node.

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:36
  • Comments:22 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
DanielRosenwassercommented, Jul 28, 2014

I assume that you have something like the following code

var d: Document;
var x = d.cloneNode(); // 'x' is of type 'Node'

but you wanted the resulting type of d.cloneNode() to be Document as well.

This is something that cannot “automatically” be expressed by our type system (nor can it be in type systems such as Java, C#, but has been “solved” in certain other languages).

One thing we could do is make cloneNode generic so that you could do:

elem.cloneNode<Document>();

and have that return a Document - but you’re not gaining much over using a type assertion.

Something that allows you to get exactly what you want would be to define your own helper function so that you generally won’t have to supply the generic parameter since it can be inferred.

function cloneNode<T extends Node>(node: T) {
    return <T>node.cloneNode();
}

var d: Document;
var x = cloneNode(d); // 'x' is of type 'Document'
2reactions
sandersncommented, Apr 2, 2020

Unfortunately, we had to revert microsoft/TSJS-lib-generator#811 because it makes type parameters that extend HTMLElement invariant where they were previously covariant. See https://github.com/microsoft/TSJS-lib-generator/pull/842

@types/tablesorter is a good test case for future attempts; clone microsoft/DefinitelyTyped and then run tsc in types/tablesorter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.cloneNode() - Web APIs - MDN Web Docs
The cloneNode() method of the Node interface returns a duplicate of the node on which this method was called. Its parameter controls if...
Read more >
Property 'id' does not exist on type 'Node' in plain JS after ...
It is essentially a TypeScript error, because cloneNode returns a Node , which, as you suspected, don't have id or classList properties. In ......
Read more >
Node.cloneNode( ): duplicate a node and, optionally, all of its ...
The cloneNode( ) method makes and returns a copy of the node on which it is called. If passed the argument true ,...
Read more >
HTML DOM Element cloneNode Method - W3Schools
Definition and Usage. The cloneNode() method creates a copy of a node, and returns the clone. The cloneNode() method clones all attributes and...
Read more >
HTML DOM cloneNode() Method - GeeksforGeeks
The HTML DOM cloneNode() Method is used to copy or clone a node on which the cloneNode() method is called. For example, a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found