fjjfjfjF
tRPC5mo ago
fjjfjfj

Output validator for Async generators on a streaming procedure using Zod

Might be a bit of a long shot but I was curious if anyone else has managed to do this?
Im just getting typescript errors as it wants the query return type to be a MaybePromise<T> when I have AsyncGenerator<T> and there doesnt seem to be anything in zod (apart from brute forcing through custom, which defeats the purpose) to make this work.
Keen to here anyones thoughts!

  test: publicProcedure.output(z.number()).query(async function*() {
    for (let i = 0; i < 10; i++) {
      yield new Promise<number>(r => setTimeout(() => r(i), i * 1000));
    }
  })

Type 'AsyncGenerator<number, void, unknown>' is not assignable to type 'MaybePromise<number>'.
Was this page helpful?