Skip to content

Wrong type inference when using response validation for a SSE stream #231

@fmanzali

Description

@fmanzali

What version of Elysia is running?

elysia 1.4.17 + eden 1.4.5 + openapi 1.4.11

What platform is your computer?

Linux 6.8.0-87-generic x86_64 x86_64

What steps can reproduce the bug?

Run this code to reproduce:

import { treaty } from "@elysiajs/eden";
import { openapi } from "@elysiajs/openapi";
import { Elysia, sse, t } from "elysia";

const model = t.Object({
  value: t.Number(),
});

const app = new Elysia()
  .use(openapi())
  .get(
    "/stream",
    function* () {
      yield sse({
        data: { value: 42 },
      });
    },
    {
      response: {
        200: t.Object({ data: model }),
      },
    }
  )
  .listen(3000);

const api = treaty(app);
const { data } = await api.stream.get();

// TS Error: Type '{ data: { value: number; }; }' must have a '[Symbol.asyncIterator]()' method that returns an async iterator.ts(2504)
// data has type of { data: { value: number; }; } instead of AsyncGenerator
for await (const chunk of data) {
  console.log(chunk);
}

What is the expected behavior?

The type of data is wrapped in a AsyncGenerator.

What do you see instead?

The type of data is just { data: { value: number }}.

Additional information

It's just a TS error: the API works as expected, and the OpenAPI docs are generated correctly.

Have you try removing the node_modules and bun.lockb and try again yet?

Yes

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions