记录错误<>打字稿/角度中的数据类型



我在typescript变量groupedAccountingLines类型上有这个错误Type '{}' is missing the following properties from type 'Record<ExportableType, PayAccountingLine[][]>': leaves, lates, corrections,我不明白为什么会得到它。

我的代码:

type ExportableType = 'leaves' | 'lates' | 'corrections';
groupedAccountingLines: Record<ExportableType, PayAccountingLine[][]>;

this.groupedAccountingLines = this.exportableTypes.reduce((acc, type) => {
const data = groupBy(this.filterLines(changes['payAccountingLines'].currentValue, changes['payPeriod'].currentValue, type), (l) => `${l.owner.employeeNumber}_${l.source.externalAccountId}`);
const groupAsArrayOfArray = Object.values(data).reduce((a, b) => [...a, b], [] as PayAccountingLine[][]);
return {
...acc,
[type]: groupAsArrayOfArray
};
}, {});

reduce返回的对象是一个包含三个键'leaves' | 'lates' | 'corrections'的对象,那么为什么我会收到这个错误呢?

提前感谢

由于这个代码段提供的类型信息有限,我猜测错误消息源于未键入的初始值。也许这样的解决方案是:

type ExportableType = 'leaves' | 'lates' | 'corrections';
groupedAccountingLines: Record<ExportableType, PayAccountingLine[][]>;

this.groupedAccountingLines = this.exportableTypes.reduce((acc, type) => {
const data = groupBy(this.filterLines(changes['payAccountingLines'].currentValue, changes['payPeriod'].currentValue, type), (l) => ${l.owner.employeeNumber}_${l.source.externalAccountId});
const groupAsArrayOfArray = Object.values(data).reduce((a, b) => [...a, b], [] as PayAccountingLine[][]);
return {
...acc,
[type]: groupAsArrayOfArray
};
}, {} as Record<ExportableType, PayAccountingLine[][]>);

最新更新