I have a recursive object structure defined as such:
interface Item {
name: string;
type: 'string' | 'url' | 'list' | 'file',
require?: boolean;
allowedFileTypes: string[];
subFields: Item[];
}
const items = {
name: "navigation",
type: "list",
subFields: [
{
name: "label",
type: "string",
defaultValue: "",
require: true,
},
{
name: "url",
type: "url",
defaultValue: "",
require: true,
},
{
name: "images",
type: "list",
subFields: [
{
name: "label",
type: "string",
defaultValue: "",
require: true,
},
{
name: "icon",
type: "file",
allowedFileTypes: ["png", "svg"],
},
],
},
],
};
I'm attempting to derive the following from this structure:
// The output would be:
type Items = {
navigation: Array<{
label: string;
url: string;
images: Array<{
label: string;
icon: 'png' | 'svg';
}>
}>
}
In a separate question, someone managed to help me use typeof to derive a flat version of this structure but I got stumped on the recursive part with TS. Here's the solution.
Basically, how can I use typeof to recursively derive my nested type based on a defined object as a generic object? Something flexible enough to also defined the required (as optional values) and for icon the png/svg options as a single string.
Is this perhaps more than TS can handle?