I'm defining two different groups: Tabs and Sections. A section exists inside tabs.(Tabs contain sections).
When declaring sections, I'd like to have the tab names to be automatically filled by the sibling tabs that were just declared.
But how to infer the names of siblings? Here's what I tried (Try it on playground):
export type TabDeclaration<TabName extends string = string> = {
name?: TabName;
};
export type SectionDeclaration<
Tabs extends TabDeclaration[],
SectionName extends string = string
> = Tabs extends TabDeclaration<infer TabName>[]
? {
name?: SectionName;
tab?: TabName;
}
: never;
type TabsAndSections<Tabs extends TabDeclaration[] = TabDeclaration[]> = {
tabs: Tabs;
sections: SectionDeclaration<Tabs>[];
};
var tabsAndSections: TabsAndSections = {
tabs: [
{
name: 'Tab 1'
},
{
name: 'Tab 2'
}
],
sections: [
{
name: 'Section 1',
tab: '' /* <---- Should autocomplete "Tab 1" or "Tab 2" */
}
]
}
tab:
should autocomplete "Tab 1" or "Tab 2".