Does TypeScript have a way to create a versatile object that can have multiple nested levels but always end with either a string or number property?
interface GenericObject {
[key: string]: string | number | GenericObject | GenericObject[];
}
const object: GenericObject = {
test: 'test',
testNum: 2,
test2: {
test: 'test'
},
test3: [
{
test: 'test'
}
]
}
While creating the object works fine, attempting to access its properties throws an error.
object.test2.test;
object.test3[0].test;
Property 'test' does not exist on type 'string | number | GenericObject | GenericObject[]'
Is there a way to make this structure work so it can be infinitely nested but always ending in either a string or a number?