/** * Copyright (c) 2023-2024 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Adam Midlik */ import { AllRequired, ParamsSchema, ValuesFor } from './params-schema.js'; /** Type of "custom" of a tree node (key-value storage with arbitrary JSONable values) */ export type CustomProps = Partial>; /** Tree node without children */ export type Node = {} extends TParams ? { kind: TKind; params?: TParams; custom?: CustomProps; ref?: string; } : { kind: TKind; params: TParams; custom?: CustomProps; ref?: string; }; /** Kind type for a tree node */ export type Kind = TNode['kind']; /** Params type for a tree node */ export type Params = NonNullable; /** Tree (i.e. a node with optional children) where the root node is of type `TRoot` and other nodes are of type `TNode` */ export type Tree = Node, TRoot extends TNode = TNode> = TRoot & { children?: Tree[]; }; /** Type of any subtree that can occur within given `TTree` tree type */ export type Subtree = NonNullable[number]; /** Type of any subtree that can occur within given `TTree` tree type and has kind type `TKind` */ export type SubtreeOfKind> = Kind>> = RootOfKind, TKind>; type RootOfKind> = Extract>>; /** Params type for a given kind type within a tree */ export type ParamsOfKind> = Kind>> = NonNullable['params']>; /** Get params from a tree node */ export declare function getParams(node: TNode): Params; /** Get custom properties from a tree node */ export declare function getCustomProps(node: Node): TCustomProps; /** Get children from a tree node */ export declare function getChildren(tree: TTree): Subtree[]; type ParamsSchemas = { [kind: string]: ParamsSchema; }; /** Definition of tree type, specifying allowed node kinds, types of their params, required kind for the root, and allowed parent-child kind combinations */ export interface TreeSchema { /** Required kind of the root node */ rootKind: TRootKind; /** Definition of allowed node kinds */ nodes: { [kind in keyof TParamsSchemas]: { /** Params schema for this node kind */ params: TParamsSchemas[kind]; /** Documentation for this node kind */ description?: string; /** Node kinds that can serve as parent for this node kind (`undefined` means the parent can be of any kind) */ parent?: (string & keyof TParamsSchemas)[]; }; }; } export declare function TreeSchema

(schema: TreeSchema): TreeSchema; /** ParamsSchemas per node kind */ type ParamsSchemasOf = TTreeSchema extends TreeSchema ? TParamsSchema : never; /** Variation of params schemas where all param fields are required */ type ParamsSchemasWithAllRequired = { [kind in keyof TParamsSchemas]: AllRequired; }; /** Variation of a tree schema where all param fields are required */ export type TreeSchemaWithAllRequired = TreeSchema>, TTreeSchema['rootKind']>; export declare function TreeSchemaWithAllRequired(schema: TTreeSchema): TreeSchemaWithAllRequired; /** Type of tree node which can occur as the root of a tree conforming to tree schema `TTreeSchema` */ export type RootFor = NodeFor; /** Type of tree node which can occur anywhere in a tree conforming to tree schema `TTreeSchema`, * optionally narrowing down to a given node kind */ export type NodeFor = keyof ParamsSchemasOf> = { [key in keyof ParamsSchemasOf]: Node[key]>>; }[TKind]; /** Type of tree which conforms to tree schema `TTreeSchema` */ export type TreeFor = Tree, RootFor & NodeFor>; export {};