3df68fa653a11e0f24197a4af7568fc116c248e1cf7d49bde5c8ebb92151658d60c1ecd60209bdbb1d7da914b9cf492e7e3dae597cde4a82a9c829e8968547 1.3 KB

123456789101112131415161718192021
  1. // Backwards-compatible iterator interfaces, augmented with iterator helper methods by lib.esnext.iterator in TypeScript 5.6.
  2. // The IterableIterator interface does not contain these methods, which creates assignability issues in places where IteratorObjects
  3. // are expected (eg. DOM-compatible APIs) if lib.esnext.iterator is loaded.
  4. // Also ensures that iterators returned by the Node API, which inherit from Iterator.prototype, correctly expose the iterator helper methods
  5. // if lib.esnext.iterator is loaded.
  6. // TODO: remove once this package no longer supports TS 5.5, and replace NodeJS.BuiltinIteratorReturn with BuiltinIteratorReturn.
  7. // Placeholders for TS <5.6
  8. interface IteratorObject<T, TReturn, TNext> {}
  9. interface AsyncIteratorObject<T, TReturn, TNext> {}
  10. declare namespace NodeJS {
  11. // Populate iterator methods for TS <5.6
  12. interface Iterator<T, TReturn, TNext> extends globalThis.Iterator<T, TReturn, TNext> {}
  13. interface AsyncIterator<T, TReturn, TNext> extends globalThis.AsyncIterator<T, TReturn, TNext> {}
  14. // Polyfill for TS 5.6's instrinsic BuiltinIteratorReturn type, required for DOM-compatible iterators
  15. type BuiltinIteratorReturn = ReturnType<any[][typeof Symbol.iterator]> extends
  16. globalThis.Iterator<any, infer TReturn> ? TReturn
  17. : any;
  18. }