elysiajs / elysia

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

Type inference of `query` params is wrong when Transforms are used #635

Closed mnpenner closed 2 months ago

mnpenner commented 2 months ago

What version of Elysia.JS is running?

elysia@1.0.18

What platform is your computer?

Linux 4.4.0-19041-Microsoft x86_64 x86_64

What steps can reproduce the bug?

Create a type transform like:

import {t} from "elysia"

export const PageToken = () => t.Transform(t.String())
    .Decode(decodeToken)
    .Encode(encodeToken)

Then type your route:

{
        query: t.Object({
            afterCursor: PageToken(),
        })
    }

What is the expected behavior?

query.afterCursor should have the type of the return value of decodeToken

What do you see instead?

query.afterCursor is typed as string (the input type coming from the client)

Additional information

I haven't looked at Elysia's source code yet, but I'm guessing Elysia us using Static to infer the type when StaticDecode should be used instead: https://github.com/sinclairzx81/typebox?tab=readme-ov-file#transform-types

There's another example of transforms on that page that's perhaps better than mine.

Edit: I think this line is the culprit:

https://github.com/elysiajs/elysia/blob/968650becd19850e4f33dca273d9e5838c937d70/src/types.ts#L205

david-plugge commented 2 months ago

Yess, this is needed!