[ts]
Argument of type '(sum: number, wallet: IConvertedRate) => number' is not assignable to parameter of type '(previousValue: number, currentValue: "" | { convertedRate: number; }, currentIndex: number, array: ("" | { convertedRate: number; })[]) => number'.
Types of parameters 'wallet' and 'currentValue' are incompatible.
Type '"" | { convertedRate: number; }' is not assignable to type 'IConvertedRate'.
Type '""' is not assignable to type 'IConvertedRate'. [2345]
不确定如何解决这些类型脚本错误
原木
这是什么
wallets
看起来像:
[
{ currency: 'USD', amount: 100 },
{ currency: 'EUR', amount: 500 }
{ currency: 'CHF', amount: 10000 }
]
这是什么
convertedRates
看起来像:
[
{ convertedRate: 100 },
{ convertedRate: 437,44 }
{ convertedRate: 9980.9 }
]
下面的代码工作并返回正确的
10518.34
,但无法消除typescript错误。
代码
interface IConvertedRate {
convertedRate: number;
}
calculateTotalValue(wallets: IWallet[]) {
const { assets } = this.props;
const convertedRates = wallets.map((wallet) => {
const conversion = assets.filter((asset) => asset.currency === wallet.currency)[0];
if (conversion) {
return {
convertedRate: roundFloat(wallet.amount * conversion.rate, 2)
};
}
return '';
});
const total = convertedRates.reduce(function(sum: number, wallet: IConvertedRate) {
return sum + wallet.convertedRate;
}, 0);
console.log('total', total);
return total.toString();
}