Files
typebox/docs/types/recursive.md

956 B

Home | Installation | Usage | Types | Values | Syntax | TypeRegistry | TypeCheck

Recursive Types

Use the Recursive function to create recursive types.

const Node = Type.Recursive(
  (This) =>
    Type.Object({
      // const Node = {
      id: Type.String(), //   $id: 'Node',
      nodes: Type.Array(This), //   type: 'object',
    }),
  { $id: "Node" }
); //   properties: {
//     id: {
//       type: 'string'
//     },
//     nodes: {
//       type: 'array',
//       items: {
//         $ref: 'Node'
//       }
//     }
//   },
//   required: [
//     'id',
//     'nodes'
//   ]
// }

type Node = Static<typeof Node>; // type Node = {
//   id: string
//   nodes: Node[]
// }

function test(node: Node) {
  const id = node.nodes[0].nodes[0].id; // id is string
}

Back to Home