Imad El Hitti
Imad El Hitti

Reputation: 937

Error on Record<> datatype in typescript/angular

I have this error Type '{}' is missing the following properties from type 'Record<ExportableType, PayAccountingLine[][]>': leaves, lates, corrections on a typescript variable groupedAccountingLines type and I can't understand why I'm getting it.

My code :

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
    };
}, {});

The object returned by the reduce is an object that contains three key 'leaves' | 'lates' | 'corrections' so why am I getting this error ?

Thank in advance

Upvotes: 0

Views: 155

Answers (1)

Mirco S.
Mirco S.

Reputation: 2610

With the limited type information this snippet delivers, my guess would be that the error message stems from the intial value which is untyped. Probably something like this would be the solution:

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[][]>);

Upvotes: 1

Related Questions