Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mnpenner opened this issue May 13, 2024 · 1 comment
Closed

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

mnpenner opened this issue May 13, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@mnpenner
Copy link
Contributor

mnpenner commented May 13, 2024

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:

? Static<NonNullable<Schema>>

@mnpenner mnpenner added the bug Something isn't working label May 13, 2024
@david-plugge
Copy link

Yess, this is needed!

SaltyAom added a commit that referenced this issue May 18, 2024
Fix #635 - Use StaticDecode<> instead of Static<> to get type of inpu…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants