I recently started using TypeScript and ran into an issue with a similar scenario. Despite my efforts, I encountered two errors that I can't seem to figure out. If anyone has any insights on how to resolve this, I would greatly appreciate the help!
interface Foo {
[key: string]: number
};
interface Bar {
[key: string]: { positive: number, negative: number }
}
// const obj: Foo | Bar = {
// usa: { positive: 5, negative: 3 },
// uk: { positive: 4, negative: 1 },
// fr: { positive: 8, negative: 2 },
// }
const obj: Foo | Bar = {
usa: 5,
uk: 3,
fr: 2,
}
Object.keys(obj).map(key => {
const val = 'positive' in obj[key] ? obj[key].positive : obj[key];
alert(val);
})
The errors that are troubling me are: