Skip to content

Union Type Response fails Typecheck when using status(code, obj) #1406

@peterlustig78

Description

@peterlustig78

What version of Elysia is running?

1.4.5

What platform is your computer?

x64

What environment are you using

bun version 1.2.22

Are you using dynamic mode?

No

What steps can reproduce the bug?

The following code fails Typecheck

const testUnionResponse = {
      200: t.Union([t.Object({
        key2: t.String(),
        id: t.Literal(2)
      }), t.Object({
        key: t.Number(),
        id: t.Literal(1)
      })])
    }

const union = new Elysia()
  .get("/test", ({status}) => { //the function definiton will error
    return status(200,{key2: "s", id:2})
  }, {
    response: testUnionResponse
  })

What is the expected behavior?

Type Check does not fail.

What do you see instead?

Type 'ElysiaCustomStatusResponse<200, { key2: string; id: 2; }, 200>' is not assignable to type 'ElysiaCustomStatusResponse<200, { key2: string; id: 2; } | { id: 1; key: number; }, 200>'.
Type '{ key2: string; id: 2; } | { id: 1; key: number; }' is not assignable to type '{ key2: string; id: 2; }'.
Property 'key2' is missing in type '{ id: 1; key: number; }' but required in type '{ key2: string; id: 2; }'.ts(2345)

Additional information

using the same code without status(200, obj) does not throw a type error.

const union2 = new Elysia()
  .get("/test", ({status}) => {
    return {key2: "s", id: 2}
  }, {
    response: testUnionResponse
  })

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