I'm currently working with JSON data that contains nested objects, and my goal is to extract the unique set of second-level keys as a type. For instance:
const json = {
'alice': {
'dogs': 1,
'birds': 4,
},
'bob': {
'dogs': 2,
'cats': 5,
},
}
type animals1 = keyof typeof json['alice'] | keyof typeof json['bob']
// animas1 = 'dogs' | 'birds' | 'cats'
type animals2 = keyof typeof json['alice' | 'bob']
// animals2 = 'dogs'
type people = keyof typeof json
type animals3 = keyof typeof json[people]
// animals3 = 'dogs'
My objective is to retrieve 'dogs' | 'birds' | 'cats'
(like in animals1) without explicitly stating the first-level keys. However, it seems like the approach I'm using (animals3) returns only the common keys rather than the union.
Is there a way to achieve this desired outcome?