microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.37k stars 12.4k forks source link

NodeList should be generic instead of having NodeListOf #53540

Open justinfagnani opened 1 year ago

justinfagnani commented 1 year ago

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.

jakebailey commented 1 year ago

FYI you probably want to report this at the repo which generates lib.dom.d.ts: https://github.com/microsoft/TypeScript-DOM-lib-generator