-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
NodeList, NodeListOf<Element> and HTMLCollection #424
Comments
Good feedback. We'll need to look at this again when we rewrite the lib.d.ts generator. |
Still being burned by this issue. getElementsByTagName (and other DOM Element selectors) seem to have a long-running history (documented here: https://bugzilla.mozilla.org/show_bug.cgi?id=14869) of returning HTMLCollection instead of NodeList. |
In many cases, we want certain sub-interface of Firstly, I suggest making those API as generic methods, e.g.: interface NodeSelector {
querySelector<E extends Element>(sel : string): E;
querySelectorAll<E extends Element>(sel : string): NodeListOf<E>;
//...
} Additionally, I suggest making the non-generic versions return a interface UniversalElement extends
HTMLAElement,
HTMLInputElement,
//...
SVGLineElement,
SVGRectElement,
//...
{}
interface NodeSelector {
//...
querySelector(sel : string): UniversalElement;
querySelectorAll(sel : string): NodeListOf<UniversalElement>;
} Rationales:
However, there is a member conflicting problem: One possible solution is also making interface UniversalClassName extends String, SVGAnimatedString {}
interface UniversalElement {
className: UniversalClassName;
} However, because primitive types in TS are not interfaces, and an instance of interface Another solution is kicking out SVG's funny |
PRs welcomed, here is some information about submitting lib.d.ts changes: https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes |
I am happy to add this. But I need to know what would be accepted. I was thinking of adding a new type modelled on NodeListOf called HTMLCollectionOf and then adding generic methods for the collection methods. For example I have these in the project I have open at the moment: interface Document
{
getElementById<THTMLElement extends HTMLElement>(elementId: string): THTMLElement;
getElementsByClassName<THTMLElement extends HTMLElement>(classNames: string): NodeListOf<THTMLElement>;
}
interface Element
{
getElementsByClassName<THTMLElement extends HTMLElement>(classNames: string): NodeListOf<THTMLElement>;
}
interface NodeSelector {
querySelector<THTMLElement extends HTMLElement>(selectors: string): THTMLElement;
querySelectorAll<THTMLElement extends HTMLElement>(selectors: string): NodeListOf<THTMLElement>;
} Not an exhaustive list but just an example I have to hand. Thoughts? |
@crash-dive this looks like can be handled in a separate PR. the OP was about 1. adding a new |
A second PR for this issue has been made. I note that |
this is my code, in this case i met following error Type 'NodeListOf' is not assignable to type 'NodeListOf'. |
Currently, the following DOM methods has a return type definition of
NodeList
, but the type of nodes returned is actually alwaysElement
:getElementsByClassName
getElementsByTagNameNS
getElementsByTagName(name: string)
getElementsByName
msElementsFromPoint
msElementsFromRect
As a result, a cast to
Element
is required before any method ofElement
can be used. To remove the need of the cast, I suggest changing their return type definition toNodeListOf<Element>
.Also, there are some HTML DOM properties (e.g.
document.forms
) having a return type definition ofHTMLCollection
, butHTMLCollection
does not extendNodeListOf<HTMLElement>
. That leads to some difficulties in creating a method that takes a list of elements. I suggest changingHTMLCollection
to extendNodeListOf<HTMLElement>
.Finally, should a
List<T>
interface with the following definition be created, such thatArray<T>
,NodeList
,NodeListOf<HTMLElement>
andHTMLCollection
all extend toList<T>
?The text was updated successfully, but these errors were encountered: