cloneNode should return sub type, not Node
See original GitHub issueWhen 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:
- Created 9 years ago
- Reactions:36
- Comments:22 (12 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
Top GitHub Comments
I assume that you have something like the following code
but you wanted the resulting type of
d.cloneNode()
to beDocument
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: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.
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 runtsc
in types/tablesorter.