elysia icon indicating copy to clipboard operation
elysia copied to clipboard

Status code NOT containing 200 breaks the Typescript check

Open bhuynhdev opened this issue 1 year ago • 0 comments

What version of Elysia.JS is running?

1.0.0-beta.5

What platform is your computer?

Linux 5.15.133.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

I am not sure if this is intended behavior, but Type checking doesn't work if the response schema doesn't contain status code 200.

Steps:

  1. Brand new project with bun create elysia test-app
  2. Change elysia version in package.json to 1.0.0-beta.5 then run bun update
  3. Change content of index.ts to below
import { Elysia, t } from "elysia";

const app = new Elysia()
  .get(
    "/apple",
    ({ error }) => {
      if (Math.random() > 0.5) {
        // Error: Object literal may only specify known properties, and 'wrong' does not exist in type '{ message: string; }'
        return error(400, { wrong: "No" });
      }
      return "Success";
    },
    {
      response: { 200: t.String(), 400: t.Object({ message: t.String() }) },
    },
  )
  .get(
    "/pear",
    ({ error, set }) => {
      if (Math.random() > 0.5) {
        // No error
        return error(400, { wrong: "No" });
      }
      set.status = 201;
      return "Created";
    },
    {
      response: { 201: t.String(), 400: t.Object({ message: t.String() }) },
    },
  )
  .listen(3000);

console.log(
  `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`,
);

Route /apple is successfully catching the error on line 8 that Object literal may only specify known properties, and 'wrong' does not exist in type '{ message: string; }'.

But route /pear doesn't catch that error, assumably because its response schema uses 201 instead of 200. If I change 201 to 200, the type checking works again. I am sure this is not a problem with my LSP, as I have restarted the LSP server multiple times, and also experiencing this same issue on another ElysiaJS code base.

image

bhuynhdev avatar Feb 18 '24 16:02 bhuynhdev