12

I have a generator method that adds asyncronously some stuff to an array and at the end yields each member with yield* array like so:

while (!done) {
      await promise;
      yield* results; //<-- the array
      results = [];  //its emptied so as to not to yield dupes
      done = !this.#eventsListened.has(eventName);
    }

However our friendly neighbor tsc reports an error and I can't even begin to understand it or how to fix it. Can anybody explain what does this error mean and how am I supposed then to yield the contents of an array?

TS2766: Cannot delegate iteration to value because the 'next' method of its iterator expects type 'undefined', but the containing generator will always send 'unknown'.

Edit: the whole generator function follows

  /**
   * Returns an iterator that loops over caught events
   * @yields Promise<Event|CustomEvent>
   */
  async *on(eventName: string, options?: AddEventListenerOptions): AsyncGenerator<Event> {
    if (this.#eventsListened.has(eventName)) {
      return;
    }
    let results: Event[] = [];

    let resolve: (value?: PromiseLike<Event> | Event) => void;

    let promise = new Promise<Event>(r => (resolve = r));
    let done = false;
    this.#eventsListened.add(eventName);

    if (typeof options === 'object' && typeof options.once !== 'undefined') {
      throw new Error('options.once is not supported. Use EventTarget2.once instead');
    }
    const callback = (evt: Event) => {
      results.push(evt);
      resolve();
      promise = new Promise<Event>(r => (resolve = r));
    };

    this.addEventListener('eventName', callback, options);
    while (!done) {
      await promise;
      yield* results;
      results = [];
      done = !this.#eventsListened.has(eventName);
    }
    this.removeEventListener(eventName, callback);
  }
6
  • Did you really mean to use yield* or do you need a normal yield? Commented Apr 13, 2020 at 22:29
  • @VLAZ I really mean yield* I want to yield each member of the array individually Commented Apr 13, 2020 at 22:31
  • 1
    Maybe related: stackoverflow.com/questions/58568399/… Commented Apr 13, 2020 at 22:36
  • More context is needed (mainly the function* definition). Commented Apr 13, 2020 at 22:38
  • The problem is that .next(stuff) will pass a value into the iterator, and as you yield* also into the array iterator. The array iterator is typed as Iterator<void, T, undefined> so you may only call it as .next(), however your generator function is typed differently (or not at all). Therefore the .next(...) calls clash. Commented Apr 13, 2020 at 22:39

1 Answer 1

15

Your main Problem is AsyncGenerator<Event> ... As you did not specify the third generic parameter, it's default value (unknown) is used, and unknown missmatches undefined.

The missmatch occurs because the inner iterator does not expect any values passed into the iterator via next(value), therefore it's TNext generic parameter is undefined. As you yield* that iterator, calling next(...) on your generators iterator will call the inner iterator with the same arguments, therefore the arguments have to match.

Typescript infers the type of a generator function to AsyncGenerator<..., ..., undefined> for exactly that reason, and you should do that too (as long as you dont want to pass values into the iterator with .next): AsyncGenerator<Event, void, undefined>.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! That did the trick... I didn't know the AsyncGenerator interface had three types. It still baffles me that you can send an iterator a value.
This has been fixed in TypeScript 5.6.2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.