【发布时间】:2021-12-09 13:29:34
【问题描述】:
我对 TypeScript 中的 reduce 方法有疑问:
const items = {a: 10, b:20, c: 30}
const itemsTotal = Object.keys(items).reduce((accumulator: number, key: keyof typeof items ) => {
return accumulator + items[key]
}, 0)
我不断收到 Typescript 错误:
'(accumulator: number, key: "a" | "b" | "c") => number' 类型的参数不能分配给 '(previousValue: string, currentValue: string, currentIndex: number , 数组:字符串[]) => 字符串'。
Types of parameters 'accumulator' and 'previousValue' are incompatible.***
看来我需要定义reduce方法的类型,但是怎么做呢?
【问题讨论】:
标签: typescript