Skip to content

Allow NodeList in for-of loop ES5 mode #4947

Closed
@saschanaz

Description

@saschanaz

NodeList interface is defined as iterable in DOM4, but by design it cannot be directly used in for-of loop because we cannot declare it as iterable in ES5.

Suggestion

Introduce a ES5-compatible way to declare iterable interfaces (Similar to what discussed in #2862)

// WebIDL-like way
interface NodeList {
  iterable<Node>
}

// extends-like way
interface NodeList iterable<Node> {}

iterable<T> in ES6 mode will work as a shorthand of [Symbol.iterator](): IterableIterator<T> but in ES5 mode will just make an internal flag to be allowed in for-of loop.

// NodeList is marked as iterable,
// it has .length property and index signature for ES5 emit, so all good
for (let node of document.querySelectorAll("...")) {
}

This will make it work both on ES5 and ES6 without changing emit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions