I'm currently working on defining a TypeScript structure for a JSON object. One part of the object includes a property called components
, which is an array of strings. However, I want to enhance this structure by adding an additional property called overrides
, where specific entries from the components
array can have property overrides.
My challenge is to find a way to restrict the componentName
key within the overrides
object to only allow values that match one of the entries in the components
array. Here's how I currently have it set up:
interface MyJsonObject {
components: string[];
overrides: {
[componentName: string]: {
[propName: string]: unknown;
};
};
}
What I want to achieve is to verify that the following configuration is valid:
const config: MyJsonObject = {
components: ['test'],
overrides: {
test: {
foo: 'bar',
}
}
}
And to ensure that the following configuration is considered invalid:
const config: MyJsonObject = {
components: ['test'],
overrides: {
baz: {
foo: 'bar',
}
}
}