elysiajs / eden

Fully type-safe Elysia client
MIT License
174 stars 41 forks source link

Typesafe isn't functioning properly on Eden Treaty 2 #58

Open hagishi opened 8 months ago

hagishi commented 8 months ago

Hi After testing various things with the RC version, it didn't work correctly. Below is the code I ran

describe("eden test", () => {
  it("derive test", async () => {
    const app = new Elysia()
      .derive(() => ({ user: "name" }))
      .get("/user", () => "user")
    treaty(app).get("/user", () => "user")
  })

  it("resolve test", async () => {
    const app = new Elysia()
      .resolve(() => ({ user: "name" }))
      .get("/user", () => "user")
    treaty(app).user.get("/user", () => "user")
  })

  it("prefix test", async () => {
    const app = new Elysia({ prefix: undefined }).get("/user", () => "user")
    treaty(app).user.user.get() //<-- this is wrong (This is how it's autocompleted.)
  })
})

capture

image

version elysia: 1.0.0-rc.15 @elysiajs/eden: 1.0.0-rc.4

hagishi commented 8 months ago

When we use { as: 'global' }, it will work properly.

describe("eden test", () => {
  it("derive local test", async () => {
    const app = new Elysia()
      .derive(() => ({ user: "name" }))
      .get("/user", ({ user }) => user);
    treaty(app).user.get(); // <- type error
  });

  it("derive global test", async () => {
    const app = new Elysia()
      .derive({ as: "global" }, () => ({ user: "name" }))
      .get("/user", ({ user }) => user);
    treaty(app).user.get(); // <- ok
  });
});
image