@sinclair/typebox
Advanced tools
Comparing version 0.17.4 to 0.17.5
{ | ||
"name": "@sinclair/typebox", | ||
"version": "0.17.4", | ||
"version": "0.17.5", | ||
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -52,2 +52,3 @@ <div align='center'> | ||
- [Reference Types](#Reference-Types) | ||
- [Recursive Types](#Recursive-Types) | ||
- [Strict](#Strict) | ||
@@ -490,2 +491,40 @@ - [Interfaces](#Interfaces) | ||
<a name="Recursive-Types"></a> | ||
### Recursive Types | ||
TypeBox provides rudimentary support for recursive types. This is handled via the `Type.Rec(...)` method. The following creates a `Node` type that contains an array of inner `nodes`. Please note that due to current recursion limits on TypeScript inference, it's currently not possible for TypeBox to statically infer for recursive types. Instead TypeBox will resolve inner recursive types as `any`. | ||
```typescript | ||
const Node = Type.Rec(Self => Type.Object({ // const Node = { | ||
id: Type.String(), // definitions: { | ||
nodes: Type.Array(Self), // self: { | ||
})) // type: 'object', | ||
// properties: { | ||
// id: { | ||
// type: 'string' | ||
// }, | ||
// nodes: { | ||
// type: 'array', | ||
// items: { | ||
// $ref: '#/definitions/self' | ||
// } | ||
// } | ||
// } | ||
// }, | ||
// $ref: '#/definitions/self' | ||
// } | ||
type Node = Static<typeof Node> // type Node = { | ||
// id: string | ||
// nodes: any[] | ||
// | ||
function walk(node: Node) { | ||
for(const inner of node.nodes) { | ||
walk(inner as Node) // Assert inner as Node | ||
} | ||
} | ||
``` | ||
<a name="Strict"></a> | ||
@@ -492,0 +531,0 @@ |
@@ -297,3 +297,5 @@ export declare const ReadonlyOptionalModifier: unique symbol; | ||
Ref<T extends TBox<TDefinitions>, K extends keyof T['definitions']>(box: T, key: K): T['definitions'][K]; | ||
/** `EXPERIMENTAL` Creates a recursive type. */ | ||
Rec<T extends TSchema>(callback: (self: TAny) => T): T; | ||
} | ||
export declare const Type: TypeBuilder; |
@@ -296,4 +296,9 @@ "use strict"; | ||
} | ||
/** `EXPERIMENTAL` Creates a recursive type. */ | ||
Rec(callback) { | ||
const self = callback({ $ref: '#/definitions/self' }); | ||
return { definitions: { self }, $ref: "#/definitions/self" }; | ||
} | ||
} | ||
exports.TypeBuilder = TypeBuilder; | ||
exports.Type = new TypeBuilder(); |
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
83236
600
703