I'm trying to come up with an interface that will behave similarly to any in that properties can be accessed with the dot notation object.foo.bar and will describe "recursive object of primitives" that
- can only have keys of type
string - can only have values of type
Primitive | Primitive[]
Where Primitive can be defined as type Primitive = string | boolean | number and by "nested object" I mean that the value can also be another recursive object of primitives.
So far I've come up with the following:
type Primitive = string | number | boolean
interface IPrimitveObject extends Record<string, IPrimitveObject | IPrimitveObject[] | Primitive | Primitive[]> {}
But this fails right on the second level of property access.
const testObject: IPrimitveObject = {
foo: 'bar',
bar: {
baz: true
}
}
testObject.bar.baz //compiler error: "Property 'bar' does not exist on type 'string | number | boolean | IPrimitveObject | IPrimitveObject[] | Primitive[]'."
Which makes sense. But how can one overcome this?