数组推送尽管声明:TS2345:'string' 类型的参数不可分配给 'never' 类型的参数

Array push despite declaration: TS2345: Argument of type 'string' is not assignable to parameter of type 'never'

我遇到了第 transactionIds: acc.transactionIds.push(currId) 行标题中提到的错误。

我的代码如下所示:

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

不知道为什么会报错。有人有想法吗? 我在网上看到我应该只声明数组(我认为我已经用 resultObject 的声明完成了)。

编辑: 当我使用传播运算符实施建议的更改时,我收到以下错误:

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'.

这里是代码:

return {
     amountAccumulated: acc.amountAccumulated + amountToAdd,
     amountLeft: acc.amountLeft - amountToAdd,
     rate: undefined,
     transactionIds: [...acc.transactionIds, currId]
 };

我无法解释为什么错误说的和它说的完全一样,但那一行是不正确的。 push returns 一个数字,不是一个数组,但是 transactionIds 应该是一个数组。

如果你想每次都创建一个新的对象,你可以分散现有的事务 ID,然后像这样添加新的:

transactionIds: [...acc.transactionIds, currId]

你说过你在让它工作时遇到问题,而且 TypeScript 仍在抱怨 never[] 数组。由于您要求 TypeScript 推断累加器的 transactionIds 类型,我猜它一定是推断错误。

我会定义一个类型:

interface ResultType {
    amountAccumulated: number;
    amountLeft: number;
    rate: number | undefined;
    transactionIds: string[];
}

然后在您为累加器提供的对象上使用它,这样 TypeScript 就可以清楚 transactionIds 的类型是什么:

const resultObject: ResultType = arrDocs.reduce(
    (acc, curr, i) => {
        // ...
    },
    <ResultType>{
        amountAccumulated: 0,
        amountLeft: spentAmount.amount,
        rate: undefined,
        transactionIds: []
    }
);

附带说明一下,为此使用 reduce 只会使它变得更加复杂。您还有很多冗余的类型注释,TypeScript 会非常乐意(正确地)推断这些注释。以下是我的处理方式:

首先,我有一个类型(尽管如果你不想,你也不必):

interface ResultType {
    amountAccumulated: number;
    amountLeft: number;
    transactionIds: string[];
    rate: number | undefined;
}

那么我会这样做:

let amountAccumulated = 0;
let amountLeft = 0;
let transactionIds: string[] = [];
for (const curr of arrDocs) {
    let currData = curr.data();
    if (currData !== undefined) {
        let amountToAdd = Math.min(
            currData.remaining_amount,
            acc.amountLeft
        );
        amountAccumulated += amountToAdd;
        amountLeft -= amountToAdd;
        transactionIds.push(curr.id);
    }
}
const resultObject: ResultType = {
    amountAccumulated,
    amountLeft,
    rate: undefined,
    transactionIds
};

如果您不想拥有类型,只需在创建对象的最后一条语句中替换它:

const resultObject: {
    amountAccumulated: number;
    amountLeft: number;
    transactionIds: string[];
    rate: number | undefined;
} = {
    amountAccumulated,
    amountLeft,
    rate: undefined,
    transactionIds
};