elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
10.45k stars 223 forks source link

Eden Treaty not generating correct type for dynamic params at root (`/:id`) #823

Open joulev opened 1 month ago

joulev commented 1 month ago

What version of Elysia is running?

1.1.12

What platform is your computer?

Darwin 23.6.0 arm64 arm

What steps can reproduce the bug?

Type check the code below.

import { edenFetch, edenTreaty, treaty } from "@elysiajs/eden";
import Elysia from "elysia";

const app = new Elysia().get("/:id", () => null);

type App = typeof app;

const edenClient = treaty<App>("http://localhost:3000");
//    ^? {}
// This doesn't work:
edenClient({ id: "1" }).get(); // This expression is not callable. Type '{}' has no call signatures.

const fetch = edenFetch<App>("http://localhost:3000");
fetch("/:id", { params: { id: "1" } }); // Works

const edenTreaty1Client = edenTreaty<App>("http://localhost:3000");
edenTreaty1Client[":id"].get(); // Works

What is the expected behavior?

edenClient should support /:id by edenClient({ id: "1" }).

What do you see instead?

Both edenFetch and the old edenTreaty have awesome support for /:id, but the new treaty doesn't. edenClient simply doesn't register the route /:id at all, and since there are no other routes here, the type of edenClient is {} and running ({ id: "1" }) on it doesn't work.

Additional information

N/A

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

Yes

Pranavtechie commented 4 weeks ago

@SaltyAom facing the exact same issue. However, I'm using a router and Ig et a different TS error.

export const classRouter = new Elysia({ prefix: '/class' })
  .get('/', () => 'hello schools')
  .get('/:userId', async ({ params: { userId } }) => {
      .....

CleanShot 2024-10-07 at 1  34 00@2x

alansikora commented 1 week ago

Is this still open/not working?