Coder Social home page Coder Social logo

Comments (2)

carrmelo avatar carrmelo commented on September 20, 2024 1

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.

sinclairzx81 avatar sinclairzx81 commented on September 20, 2024

@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.

TypeScript Link Here

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.