Repository navigation
Missing string indexer on NamedNodeMap interface in lib.dom.d.ts? #30928
Description
Activity
- changed the title
[-]Missing string indexer on NamedNodeMap?[/-][+]Missing string indexer on NamedNodeMap interface in lib.dom.d.ts?[/+]on Apr 15, 2019 - addedBugA bug in TypeScriptA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScript
on Apr 17, 2019 There are two indices specified; a numeric and a string index:
"NamedNodeMap object’s supported property indices are the numbers in the range zero to its attribute list’s size minus one, unless the attribute list is empty, in which case there are no supported property indices."
This is the numeric index; typical
ArrayLike<Attr>."A NamedNodeMap object’s supported property names are the return value of running these steps:
- Let names be the qualified names of the attributes in this NamedNodeMap object’s attribute list, with duplicates omitted, in order.
- If this NamedNodeMap object’s element is in the HTML namespace and its node document is an HTML document, then for each name in names:
- Let lowercaseName be name, in ASCII lowercase.
- If lowercaseName is not equal to name, remove name from names.
- Return names."
This is the named string index. It's case-sensitive in SVG documents/elements, forced-lowercase in HTML documents.
We can't represent this in typescript, but only the numeric indices are enumerable. Any string index is
enumerable: false. This also means thatObject.valueswould use the wrong overload! Fortunately both indices (where defined) haveAttrinstances.Reacted by Nick Taylor, fregante and Sindre Sorhussaschanaz commented
on Apr 26, 2019 ContributorMore actionsAFAIK this is intentionally disabled because TS didn't support string indexer when any other member has a different type than the indexer returns.
But looks like somehow that behavior changed.Still causes errors when creating new types.I ran into this today porting some old code relying on the format
element.attributes[name].value.Maybe the string index should actually be
interface NamedNodeMap { [key: string]: Attr | undefined; }
? Otherwise it will thing every access of any string results in
Attr, which isn't the case. Should this be applied to number indices too?- addedDesign LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixedand removedBugA bug in TypeScriptA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScript
on Aug 25, 2026 RyanCavanaugh commented
on Aug 25, 2026 MemberMore actionsWe can't add an illegal string indexer, since this interface has other members that aren't
Attr(e.g.x.setNamedItem). We'd need support for "rest" object signatures, which doesn't exist yet.
For context, see refined-github/refined-github#1783, specifically this commit, refined-github/refined-github@48baae0.
Also, just posting my tweet about this so that people can find discussions about the issue. See https://twitter.com/nickytonline/status/1117524648982392833
I'll restate what I tweeted as I probably should have posted my question here originally.
The NamedNodeMap interface in lib.dom.ts does not allow for a string indexer, even though vanilla JS supports this in browsers, e.g.
someDomElement.attribute['aria-label'].value.We have code like this in the Refined GitHub extension, so for the time being, I've gone ahead via a declaration merge for
NamedNodeMapI can't tell from the MDN docs for NamedNodeMap if it's standard or not. All they seem to mention is "Attr nodes' indexes may differ among browsers" which wouldn't apply to access by the attribute name.
Just wondering if this was omitted by mistake or is it because this is not considered WHATWG DOM standard? I went to https://dom.spec.whatwg.org/#interface-namednodemap and unless I'm reading it incorrectly, I believe it states that using a string indexer is valid.
Thoughts? Happy to PR this up if it's valid.