I am new to typescript and learning typescript by writing it.
I am trying below code but it is not letting me access the field of the type passed to function.
Code :
interface Overview1 {
name: string;
family: string;
}
interface Overview2 {
name: string;
storage: string;
}
type OverView = Overview1 | Overview2;
function LogKeyValuePair(data: OverView) {
const keys : string[] = Object.keys(data);
keys.forEach((key) => {
console.log(data[key])
})
}
const o2 = {
name: "john",
storage: "10 gb"
}
LogKeyValuePair(volume)
Error: error is at this line console.log(data[key])
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'OverView'. No index signature with a parameter of type 'string' was found on type 'OverView'.