Is it possible to have a function that takes a configuration object as its parameter, specifying which properties in a data object should be read? The configuration object has two properties that correspond to keys in the data object.
The configuration object is structured like this:
const config = {
rootPropName: 'root',
relationPropName: 'children',
}
The data object is flat but can form nested structures based on the specified relationPropName
.
type ValidKeyTypes = string | number | symbol;
type Input = {
[key in ValidKeyTypes]: {
name: string,
children: (keyof Dummy)[]
}
}
const data: Input = {
abc: {
name: 'ABC',
children: []
},
def: {
name: 'DEF',
children: ['ghi']
},
ghi: {
name: 'GHI',
children: []
},
root: {
name: 'ROOT',
children: ['abc', 'def']
}
}
Main function
type Config<T> = {
rootPropName: keyof T;
relationPropName: keyof T[keyof T];
}
const iterateFlatObject = <T extends object>(config: Config<T>, data: T) => { ... }
Using main function
iterateFlatObject<Input>(config, data);
This code snippet results in an error message regarding type compatibility when configuring the function. This leads to the question of how to create a dynamic configuration type that provides accurate autocompletion and validates properties against the provided data object.
- Suggestions on improving functionality are appreciated.
Please reach out if further clarification is needed.