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

incorrect return type of NodeListOf.item() #38529

Open Oloompa opened 4 years ago

Oloompa commented 4 years ago

typescript version: 3.9.2

method item() of nodelist should return a node or null (if index out of range) (see: https://developer.mozilla.org/en-US/docs/Web/API/NodeList/item)

Typescript behavior is incorrect (lib.dom.d.ts)

interface NodeListOf<TNode extends Node> extends NodeList {
    item(index: number): TNode;
}
RyanCavanaugh commented 4 years ago

This is consistent with the current behavior of arrays (see #13778) so it's perhaps awkward to change.

Oloompa commented 4 years ago

But current typescript behavior is not the real behavior of javascript.

nickhudkins commented 2 years ago

@RyanCavanaugh a NodeList is not an array though. It is a NodeList, which happens to be able to be indexed (like an array), but .item is a function that returns Element | null per the spec: https://developer.mozilla.org/en-US/docs/Web/API/NodeList/item

This is not just awkward, it is incorrect.

trusktr commented 2 years ago

TS now has | undefined for indexed access. This should have followed that probably.