Skip to content

NodeList, NodeListOf<Element> and HTMLCollection #424

Closed
@FranklinWhale

Description

@FranklinWhale

Currently, the following DOM methods has a return type definition of NodeList, but the type of nodes returned is actually always Element:

getElementsByClassName
getElementsByTagNameNS
getElementsByTagName(name: string)
getElementsByName
msElementsFromPoint
msElementsFromRect

As a result, a cast to Element is required before any method of Element can be used. To remove the need of the cast, I suggest changing their return type definition to NodeListOf<Element>.

Also, there are some HTML DOM properties (e.g. document.forms) having a return type definition of HTMLCollection, but HTMLCollection does not extend NodeListOf<HTMLElement>. That leads to some difficulties in creating a method that takes a list of elements. I suggest changing HTMLCollection to extend NodeListOf<HTMLElement>.

Finally, should a List<T> interface with the following definition be created, such that Array<T>, NodeList, NodeListOf<HTMLElement> and HTMLCollection all extend to List<T>?

interface List {
    length: number;
    [index: number]: T;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issueHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions