microsoft / microsoft/TypeScript

NodeList should be generic instead of having NodeListOf

Open
#53,540 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

lib Update Request

I may be missing something, but I don't see why NodeListOf exists instead of just having NodeList itself be generic. Maybe it's a vestige from a time before generic parameter defaults.

NodeList should be able to be generic with a default value of Node.

Configuration Check

afaik, this is true of all versions of lib.dom.ts. I've checked up to 5.0

Missing / Incorrect Definition

I think this should be a backwards compatible change:

interface NodeList<TNode extends Node = Node> {
  readonly length: number;
  item(index: number): TNode | null;
  forEach(callbackfn: (value: TNode, key: number, parent: NodeList<TNode>) => void, thisArg?: any): void;
  [index: number]: TNode;
}

/** @deprecated Use NodeList */
interface NodeListOf<TNode extends Node> extends NodeList<TNode> {
  item(index: number): TNode;
}

Sample Code

let getDivs = (): NodeList<HTMLDivElement> => {/* ... */};

Documentation Link

N/A though removing NodeListOf would remove a type that's only present in lid.dom.d.ts, not the actual DOM APIs.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib.dom.ts and locate the NodeList and NodeListOf declarations. Compare them with the requested generic default and deprecated compatibility alias; the work is done when NodeList supports the shown HTMLDivElement usage while existing NodeListOf code remains compatible.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.