Is there a method to iterate through all the values of nested objects in Typescript when the nested objects have different types (a
and b
below) without using any
?
I attempted the following
const obj = {
a: {
foo: 123,
bar: 456
},
b: {
gg: 789
}
}
let key: keyof typeof obj
for (key in obj) {
const subobj = obj[key];
let subkey: keyof typeof subobj;
for (subkey in subobj) {
subobj[subkey] = 100;
}
}
however, the typechecker indicates that both subkey
and subobj
are inferred as never
The left-hand side of a 'for...in' statement must be of type 'string' or 'any'
let subkey: never
Type 'number' is not assignable to type 'never'.
const subobj: {
foo: number;
bar: number;
} | {
gg: number;
}