So here's a simplified testcase of what I'm fighting:
const testCase = {a:{b:"result"}}
for (const i in testCase) {
console.log("i", i)
for (const j in testCase[i]){
console.log("j", j)
}
}
This fails with:
error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ a: { b: string; }; }'.
No index signature with a parameter of type 'string' was found on type '{ a: { b: string; }; }'.
5 for (const j in testCase[i]){
I can work around this by casting i
as a keyof typeof testCase
.
const testCase = {a:{b:"result"}}
for (const i in testCase) {
let typedIndex = i as keyof typeof testCase
console.log("i", i)
for (const j in testCase[typedIndex]){
console.log("j", j)
}
}
However, this gets cumbersome depending on how many sub-ojbects I need to iterate through? Is there a better way to deal with this or am I just heading towards an anti-pattern of not having a predefined shape for testCase
.