I am dealing with an object that contains nested arrays, structured like this:
export class OrdenCompra {
public id?: number,
public insumos?: OrdenCompraInsumo[],
}
export class OrdenCompraInsumo {
id?: number;
traslados?: IImpuestoTraslado[];
}
export class ImpuestoTraslado{
public id?: number,
public impuesto?: number
}
I am trying to add a value to the array named
traslados
as shown below
const retencion = new ImpuestoRetencion();
ordenCompra?.insumos[index]?.retenciones?.push(retencion);
However, the issue is that at this point,
ordenCompra?.insumos[index]?.retenciones?
it is currently
undefined
resulting in the value not being assigned. I have attempted to initialize it but keep encountering errors, such as:
ordenCompra?.insumos[index]?.retenciones = []
or
ordenCompra?.insumos[index]?.retenciones = ImpuestoRetencion[];
or
ordenCompra?.insumos[index]?.retenciones: ImpuestoRetencion[] || [];
Each attempt returns the error message:
The left-hand side of an assignment expression may not be an optional property access.
As a result, I have been unable to successfully assign a value to this array. While I understand this may seem like a basic question, I have spent hours searching for a solution without success.