Despite conducting a thorough search about this error online, I still haven't been able to find a solution.
Let's jump into an example with data that looks like this:
const earthData = {
distanceFromSun: 149280000,
continents: {
asia: {area: 44579000, population: 4560667108},
africa: {area: 30370000, population: 1275920972},
europe: {area: 10180000, population: 746419440},
america: {area: 42549000, population: 964920000},
australia: {area: 7690000, population: 25925600},
antarctica: {area: 14200000, population: 5000}
}
}
I am trying to create a new object where the keys are continent names and the values are areas. The following code works flawlessly for this:
const outputWorks = Object.fromEntries(Object.entries(earthData.continents).map( ([k, o]) => [k, o.area] ))
// {
// "asia": 44579000,
// "africa": 30370000,
// "europe": 10180000,
// "america": 42549000,
// "australia": 7690000,
// "antarctica": 14200000
// }
However, when using similar code on different input data, it doesn't work as expected.
const solarSystem = {
mercury: {},
venus: {},
earth: {
distanceFromSun: 149280000,
continents: {
asia: { area: 44579000, population: 4560667108 },
africa: { area: 30370000, population: 1275920972 },
europe: { area: 10180000, population: 746419440 },
america: { area: 42549000, population: 964920000 },
australia: { area: 7690000, population: 25925600 },
antarctica: { area: 14200000, population: 5000 },
},
},
mars: {},
jupiter: {},
saturn: {},
uranus: {},
neptun: {},
};
const earthDataExtracted = Object.values(solarSystem)[2] as any; // same as `earthData`
Why does the following generate an error in this case?
Object.fromEntries(Object.entries(earthDataExtracted.continents).map( ([k, o]) => [k, o.area] ))
// ^
(parameter) o: unknown
Object is of type 'unknown'.(2571)
Interestingly, the error message differs on my local VSCode instance:
Property 'area' does not exist on type 'unknown'.ts(2339)
Am I overlooking something simple here?