Comments (2)
Thank you for all the input, S 🚀 . I have adjusted the definitions to your examples using Type.Ref, and even if I still get the error The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.ts(7056)
, at least I'm able to define it by types and not in just one block.
I will give it a go to typebox-workbench
and continue the investigation over TS.
Thanks again! I'll close as this is more a question than an issue, and your answer is complete
from typebox.
@carrmelo Hiya,
TypeBox only has limited support for mutual recursive schematics. While you can express the Json Schema to capture mutual recursion, the type inference support is limited. The following is a best effort attempt to represent these types using TypeBox (using heavy use of Type.Ref to work around order dependency (or order of definitions)), but mileage may vary depending on the actual types you're trying to represent.
import { Type, Static } from '@sinclair/typebox'
// interface A {
// b?: B[];
// d?: D;
// }
const A = Type.Object({
b: Type.Optional(Type.Array(Type.Ref<typeof B>('B'))),
d: Type.Optional(Type.Ref<typeof D>('D'))
}, { $id: 'A' })
// interface B {
// b?: B[];
// c?: C;
// d?: D;
// }
const B = Type.Recursive(This => Type.Object({
b: Type.Optional(Type.Array(This)),
c: Type.Optional(Type.Ref<typeof C>('C')),
d: Type.Optional(Type.Ref<typeof D>('D'))
}), { $id: 'B' })
// export type C = {
// containers?: Record<string, A>;
// };
const C = Type.Object({
containers: Type.Optional(Type.Record(Type.String(), Type.Ref('A')))
}, { $id: 'C' })
// interface D {
// items: E[];
// }
const D = Type.Object({
items: Type.Array(Type.Ref<typeof E>('E'))
}, { $id: 'D' })
// Missing
const G = Type.Object({}, { $id: 'G' })
// Missing
const H = Type.Object({}, { $id: 'H' })
// type E = F | G | H;
const E = Type.Union([Type.Ref('F'), G, H], { $id: 'E' })
// interface F extends B { }
const F = Type.Composite([B, Type.Object({})], { $id: 'F' })
Generally, if you can avoid mutually recursive types, that's going simplify things a lot. Also note that while the Json format can encode mutual recursive structures, you will need terminating types (like Null) that will ensure the mutually recursive structure can terminate which helps to avoid infinite loops when type checking (this is a bit of a deep topic)
// value.node.node.node.node.node.node.node.....infinity
const T = Type.Recursive(This => Type.Object({
node: This
}))
const T = Type.Recursive(This => Type.Object({
node: Type.Union([Type.Null(), This]) // note that the null union option means this type can terminate.
}))
So based on your original type example, there is a lot going on here. If it's helpful you can try use https://sinclairzx81.github.io/typebox-workbench to author your types. This will let you model types using TS syntax, with the generated TB type matching up. Just keep in mind that the workbench requires you to define types in order (so you have to define the type before using it in other types). It may be useful to reshape your types in a way that's friendly to TypeBox.
Hope this helps
S
from typebox.
Related Issues (20)
- Unable to use a union of literal as record key type after upgrading version HOT 2
- Unknown format 'email' after update HOT 3
- Uncaught Error: Cannot access 'Object' before initialization HOT 3
- Support for conditional optional based on other value HOT 3
- Type.Mapped does not handle optional properties the same as Typescript HOT 2
- Add changelog or create a new release for each tag HOT 3
- TypeCheck.Decode return type inferred incorrectly? HOT 12
- Feature request: functions as defaults HOT 3
- Composite doesn't check additionalProperties HOT 2
- Custom TypeRegistry and TypeFormat HOT 1
- [feat] describe a object with pattern key, and literal key at the same time HOT 1
- Easier/general way to implement Typescript's "key remapping via `as`" approach HOT 1
- Overriding Schema Title HOT 1
- Cannot find module "./assert/index.mjs" HOT 1
- Encode is called twice on Type.Transform when inner type is Type.Intersection HOT 1
- StaticDecode cannot handle union of literals, while Static can HOT 1
- How to validate raw json schema HOT 2
- Why is Nullable not a first class Type? HOT 4
- Value.Convert does not seem to convert sometimes HOT 3
- `Value.Default` corrupts data in unions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typebox.