11import { JsonNode , ConNode , ValNode , ObjNode , VecNode , StrNode , BinNode , ArrNode } from "../nodes" ;
22import { NodeBuilder , s } from "../../json-crdt-patch" ;
3+ import type { JsonNodeToSchema } from "./types" ;
34
45/**
56 * Converts any JSON CRDT node to a schema representation. The schema can be
@@ -11,27 +12,27 @@ import {NodeBuilder, s} from "../../json-crdt-patch";
1112 * @param node JSON CRDT node to recursively convert to schema.
1213 * @returns Schema representation of the JSON CRDT node.
1314 */
14- export const toSchema = ( node : JsonNode ) : NodeBuilder => {
15- if ( node instanceof ConNode ) return s . con ( node . val ) ;
16- if ( node instanceof ValNode ) return s . val ( toSchema ( node . node ( ) ) ) ;
15+ export const toSchema = < N extends JsonNode < any > > ( node : N ) : JsonNodeToSchema < N > => {
16+ if ( node instanceof ConNode ) return s . con ( node . val ) as any ;
17+ if ( node instanceof ValNode ) return s . val ( toSchema ( node . node ( ) ) ) as any ;
1718 if ( node instanceof ObjNode ) {
1819 const obj : Record < string , NodeBuilder > = { } ;
1920 node . nodes ( ( child , key ) => obj [ key ] = toSchema ( child ) ) ;
20- return s . obj ( obj ) ;
21+ return s . obj ( obj ) as any ;
2122 }
2223 if ( node instanceof VecNode ) {
2324 const arr : NodeBuilder [ ] = [ ] ;
2425 node . children ( ( child ) => arr . push ( toSchema ( child ) ) ) ;
25- return s . vec ( ...arr ) ;
26+ return s . vec ( ...arr ) as any ;
2627 }
27- if ( node instanceof StrNode ) return s . str ( node . view ( ) ) ;
28- if ( node instanceof BinNode ) return s . bin ( node . view ( ) ) ;
28+ if ( node instanceof StrNode ) return s . str ( node . view ( ) ) as any ;
29+ if ( node instanceof BinNode ) return s . bin ( node . view ( ) ) as any ;
2930 if ( node instanceof ArrNode ) {
3031 const arr : NodeBuilder [ ] = [ ] ;
3132 node . children ( ( child ) => {
3233 if ( child ) arr . push ( toSchema ( child ) ) ;
3334 } ) ;
34- return s . arr ( arr ) ;
35+ return s . arr ( arr ) as any ;
3536 }
36- return s . con ( undefined ) ;
37+ return s . con ( undefined ) as any ;
3738} ;
0 commit comments