What I'm attempting to do next is calculate the total value of value
in pounds. However, this is causing an error message stating:
TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'MoneyType'. No index signature with a parameter of type 'string' was found on type 'MoneyType'
.
interface MoneyType {
yuan: number,
dollar: number,
pound: number,
}
const CONVERSION_RATE: MoneyType = {
yuan: 10,
dollar: 5,
pound: 1
}
function totalValue(value: MoneyType) {
let totalValue = 0;
for (let moneyType in value) {
totalValue += CONVERSION_RATE[moneyType] * value[moneyType];
}
return totalValue;
}
While I know that I could use
interface MoneyType {[key: string]: number}
as an alternative, I'm curious if there's a way to maintain the specific key restrictions to yuan, dollar, and pound.