Last active
October 18, 2019 21:58
-
-
Save kbingman/2900dc06876ca226a203b9004472da7c to your computer and use it in GitHub Desktop.
Demo Node structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.