Closed liyofx closed 7 months ago
@liyofx Hiya,
TypeBox does not support cyclic types. The issue here is that these type functions are cyclically initializing each other. You can reproduce the issue by calling the userFindMany()
function only.
userFindMany() // results in a cyclic type
// userFindMany -> UserSelectObjectSchema ->
// bookFindMany -> BookSelectObjectSchema -> UserArgsObjectSchema -> UserSelectObjectSchema ->
// bookFindMany -> BookSelectObjectSchema -> UserArgsObjectSchema -> UserSelectObjectSchema ->
// bookFindMany -> BookSelectObjectSchema -> UserArgsObjectSchema -> UserSelectObjectSchema ->
// bookFindMany -> ...infinite
You can somewhat approach cyclic types using Type.Ref
however cyclic type inference is not well supported in TypeBox (due to difficulties getting TypeScript to handle infinite instantiations). Additionally, cyclic, self referential values are non-serializable in JSON.
The recommendation for handling these types would be use either use Type.Ref
or just explicitly implement a custom type and register via the TypeRegistry. Another approach may be to use a generic recursive type, but would likely require refactoring (or restructuring the type to support)
Will close off this issue as this functionality is currently not supported. All the best S
@sinclairzx81 I tried Type.Ref, but it still didn't work. I hope you can give me a simple example. Thank you.