Skip to content

Treaty query type inference collapses to route level when not all macros define query schema #260

Description

@dodiz

What version of Elysia is running?

1.4.28

What platform is your computer?

Linux 7.0.3-arch1-2 x86_64 unknown

What steps can reproduce the bug?

When using multiple macros, the query object in treaty inference collapses to route level only, if not all the macros provide a resolve function and a query schema, (empty schema is also fine)`

minimal reproducible example

import Elysia, { t } from "elysia";

const app = new Elysia()
  .macro("macro1", {
    query: t.Object({
      macro1QueryParam: t.String(),
    }),
    resolve: ({ query }) => {
      return {
        macro1Params: query,
      };
    },
  })
  .macro("macro2", {
    resolve: () => {
      return {
        user: "username",
      };
    },
  })
  .get(
    "/",
    ({ query, macro1Params, user }) => {
      return {
        macro1Params,
        user,
        query,
      };
    },
    {
      macro1: true,
      macro2: true,
      query: t.Object({
        routeParam: t.String(),
      }),
    },
  );

import { treaty } from "@elysia/eden";

const client = treaty<typeof app>("");

await client.get({
  query: {
    macro1QueryParam: "",
    //?^ Object literal may only specify known properties, and 'macro1QueryParam' does not exist in type '{ routeParam: string; }'.
    routeParam: "",
  },
});

My temporary fix is to add an empty query schema object inside macro2
query: t.Object({}),, this make the inference work fine

What is the expected behavior?

marco1QueryParam should be defined

What do you see instead?

//?^ Object literal may only specify known properties, and 'macro1QueryParam' does not exist in type '{ routeParam: string; }'.

Additional information

No response

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

I reproduced the bug in a new environment

Metadata

Metadata

Assignees

No one assigned

    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