I have the error mentioned in the title for the line transactionIds: acc.transactionIds.push(currId)
.
My code looks like this:
const resultObject: {
amountAccumulated: number;
amountLeft: number;
rate: number | undefined;
transactionIds: string[];
} = arrDocs.reduce(
(acc, curr, i) => {
let currData:
| admin.firestore.DocumentData
| undefined = curr.data();
if (currData === undefined) return acc;
let currId = curr.id;
let amountLeft: number = acc.amountLeft;
let amountToAdd: number = Math.min(
currData.remaining_amount,
amountLeft
);
return {
amountAccumulated: acc.amountAccumulated + amountToAdd,
amountLeft: acc.amountLeft - amountToAdd,
rate: undefined,
transactionIds: acc.transactionIds.push(currId)
};
},
{
amountAccumulated: 0,
amountLeft: spentAmount.amount,
rate: undefined,
transactionIds: []
}
);
I don't know why I have the error. Anyone with an idea?
I read online that I should just declare the array (which I thought I have done with the declaration of the resultObject
).
Edit: When I implement the suggested change using the spread operator, I receive the following error:
TS2345: Argument of type '(acc: { amountAccumulated: number; amountLeft: number; rate: undefined; transactionIds: never[]; ...' is not assignable to parameter of type '(previousValue: { amountAccumulated: number; amountLeft: number; rate: undefined; transactionIds:...'.
Type '{ amountAccumulated: number; amountLeft: number; rate: undefined; transactionIds: string[]; }' is not assignable to type '{ amountAccumulated: number; amountLeft: number; rate: undefined; transactionIds: never[]; }'.
Types of property 'transactionIds' are incompatible.
Type 'string[]' is not assignable to type 'never[]'.
Type 'string' is not assignable to type 'never'.
Here the code:
return {
amountAccumulated: acc.amountAccumulated + amountToAdd,
amountLeft: acc.amountLeft - amountToAdd,
rate: undefined,
transactionIds: [...acc.transactionIds, currId]
};