NGXS使用存储操作符更新对象数组,其中包含对象数组

时间:2019-12-04 18:32:27

标签: angular ngxs

为我的对象考虑以下类型定义:

export interface Payment {
    readonly id: string;
    readonly taxes: ReadonlyArray<Datum>;
}

export interface Datum {
    readonly type: string;
    readonly value: number;
}

我的状态如下:

const data: Payment[] = [
    {
        id: '1',
        taxes: [
            {
                type: 'One',
                value: 123
            },
            {
                type: 'Two',
                value: 123
            }
        ]
    },
    {
        id: '2',
        taxes: [
            {
                type: 'One',
                value: 456
            },
            {
                type: 'Two',
                value: 456
            }
        ]
    }
];

我想发出一条将更新特定付款税的命令。它看起来如下:

export class UpdateTax {
    public static readonly type = '[Payments] Update Tax';
    constructor(public readonly id: string, public readonly type: string, public readonly value: number) {}
}

如何使用NGXS运算符更新特定的税收对象?我可以使用扩展运算符并手动将其全部映射完成,这没问题,但是我很好奇如何使用状态运算符来实现。

我的尝试是:

setState(
    patch({
        payments: updateItem(
            p => !!p && p.id === id,
            patch({
                taxes: updateItem(t => !!t && t.type === type, patch({ value }))
            })
        )
    })
);

这很奇怪,但是显示了以下错误:

Argument of type '<U extends PatchValues<{ taxes: Datum[] | StateOperator<Datum[]>; }>>(existing: Readonly<U>) => U' is not assignable to parameter of type 'Payment | StateOperator<Payment>'.
  Type '<U extends PatchValues<{ taxes: Datum[] | StateOperator<Datum[]>; }>>(existing: Readonly<U>) => U' is not assignable to type 'StateOperator<Payment>'.
    Types of parameters 'existing' and 'existing' are incompatible.
      Type 'Readonly<Payment>' is not assignable to type 'Readonly<PatchValues<{ taxes: Datum[] | StateOperator<Datum[]>; }>>'.
        Types of property 'taxes' are incompatible.
          Type 'readonly Datum[]' is not assignable to type 'Datum[] | StateOperator<Datum[]> | undefined'.
            The type 'readonly Datum[]' is 'readonly' and cannot be assigned to the mutable type 'Datum[]'.

我已在tsconfig中启用了严格模式。

0 个答案:

没有答案