-
Notifications
You must be signed in to change notification settings - Fork 152
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.Recursive does not work with Type.Transform for StaticDecode #895
Comments
Hey, I worked on reproduction and my own report before finding there's a report already. My issue seems to be the same. There's an additional issue reproduction I've prepared https://github.com/dearlordylord/typebox-recursion-transform-bug/blob/master/libs%2Ftypebox%2Fsrc%2Flib%2Ftypebox.ts
when it comes to .Transform inside .Recursive, the type seems to be erased to |
@joshuaavalon Hi, apologies for the delay. So, the problem here is that the const R = Type.Recursive(self => Type.Object({
x: Type.Transform(self) // The self type is unresolved at this callsite. Because of this, the transform
.Decode(value => value) // holds onto the unresolved type, which infers as never on decode.
.Encode(value => value),
// ^ error here
})) // self is only known here Unfortunately, there isn't an trivial fix for inferring transforms using unresolved recursive types. However you can invert the type by moving the transform to the outside. import { Type, StaticDecode, StaticEncode } from '@sinclair/typebox'
const R = Type.Recursive(self => Type.Object({
x: Type.Array(self)
}))
const T = Type.Transform(R) // self is evaluated in R
.Decode(value => 1 as const)
.Encode(value => ({ x: [{ x: [{ x: [] }] }] }))
type D = StaticDecode<typeof T> // type D: 1
type E = StaticEncode<typeof T> // type E: { x: ...[] } Hope this lends some insight into the issue. I don't think this is going to be resolvable under the current design, so will close this one out as non-actionable. I would encourage you to try the second approach though and see if it meets your requirements. Happy to discuss more on this thread if you have any follow up questions. Cheers |
@sinclairzx81 I know this problem may not be solvable when I tried to investigate. Unfortunately, you workaround do not solve my problem because the example is a simplified case. The Anyway, thanks for your investigation. |
You can see
b
is resolved toundefined
even though it should beB | undefined
.The text was updated successfully, but these errors were encountered: