Skip to content

Instantly share code, notes, and snippets.

@kbingman
Last active October 18, 2019 21:58
Show Gist options
  • Save kbingman/2900dc06876ca226a203b9004472da7c to your computer and use it in GitHub Desktop.
Save kbingman/2900dc06876ca226a203b9004472da7c to your computer and use it in GitHub Desktop.
Demo Node structure
type UUID = string;
interface BaseNode {
uuid: UUID;
path: string; // parent-uuid/uuid
type: "node" | "section" | "group" | "text";
allowedChildren?: ContentNodes; // Optional because the actual instances of Nodes do not need this
children?: UUID[]; // not all Nodes have children
}
interface SectionNode extends BaseNode {
type: "section";
allowedChildren?: GroupNode | TextBlock; // Could be moved to ValidChildren instead
}
interface GroupNode extends BaseNode {
type: "group";
allowedChildren?: TextBlock; // Could be moved to ValidChildren instead
}
interface TextBlock extends BaseNode {
type: "text";
allowedChildren?: never; // Could be moved to ValidChildren instead
}
type ContentNodes = SectionNode | GroupNode | TextBlock;
export interface ValidChildren {
section: SectionNode["allowedChildren"]; // GroupNode | TextBlock
group: GroupNode["allowedChildren"]; // TextBlock
text: TextBlock["allowedChildren"]; // never
}
type AssertValidChild<
P extends ContentNodes,
C extends ContentNodes
> = C extends ValidChildren[P["type"]]
? unknown
: {
"invalid child element for": P;
allowedChildren: ValidChildren[P["type"]];
};
const text: TextBlock = {
uuid: '2345',
path: '/section-123/group-1234/text-2345',
type: 'text'
};
const group2: GroupNode = {
uuid: '1235',
path: '/section-123/group-1234',
type: 'group',
children: []
};
const group1: GroupNode = {
uuid: '1234',
path: '/section-123/group-1235',
type: 'group',
children: [text]
};
const section: SectionNode = {
uuid: '123',
path: '/section-123',
type: 'section',
children: [text, group1]
};
@kbingman
Copy link
Author

This is the start of our basic node structure (minus any data that actually means anything).

There is some additional work that allows a check of the allowed children.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment