[英]Getting error when trying to initialize nested array "The left-hand side of an assignment expression may not be an optional property access."
I have an object whith an array and inside this a second array, just like this.我有一个 object 带有一个数组,在第二个数组里面,就像这样。
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 want to add some value to我想增加一些价值
traslados
特拉斯拉多斯
like this像这样
const retencion = new ImpuestoRetencion();
ordenCompra?.insumos[index]?.retenciones?.push(retencion);
but at this point但此时
ordenCompra?.insumos[index]?.retenciones?
ordenCompra?.insumos[index]?.retenciones?
it is这是
undefined
不明确的
so the value it is never assigned.所以它的值永远不会被分配。
if i try to initialize i get an error.如果我尝试初始化我会得到一个错误。
ordenCompra?.insumos[index]?.retenciones = []
ordenCompra?.insumos[索引]?.retenciones = []
or或者
ordenCompra?.insumos[index]?.retenciones = ImpuestoRetencion[];
ordenCompra?.insumos[index]?.retenciones = ImpuestoRetencion[];
or或者
ordenCompra?.insumos[index]?.retenciones: ImpuestoRetencion[] ||
ordenCompra?.insumos[index]?.retenciones: ImpuestoRetencion[] || [];
[];
which says它说
The left-hand side of an assignment expression may not be an optional property access.
赋值表达式的左侧可能不是可选的属性访问。
so i have not been able to assign a value to this array.所以我无法为这个数组赋值。 i know this is a very trivial question but even when i have search for hours.
我知道这是一个非常微不足道的问题,但即使我搜索了几个小时。
?.
, known as optional chaining is only useful for reading/invoking, not for setting. ,称为可选链接,仅对读取/调用有用,对设置无效。 From the docs :
从文档:
At its core, optional chaining lets us write code where TypeScript can immediately stop running some expressions if we run into a
null
orundefined
.在其核心,可选链接让我们编写代码,如果我们遇到
null
或undefined
, TypeScript 可以立即停止运行某些表达式。
So you might interpret const foobarbaz = foo?.bar?.baz
like this:所以你可能会这样解释
const foobarbaz = foo?.bar?.baz
:
// Will have type: undefined | typeof baz
const foobarbaz = foo === undefined || foo === null
? undefined
: foo.bar === undefined || foo.bar === null
? undefined
: foo.bar.baz
This makes no sense in the context of a set because you cannot set null or undefined to a value:这在集合的上下文中没有任何意义,因为您不能将 null 或 undefined 设置为一个值:
foo?.bar?.baz = foobarbaz
// ^^^^^^^^^^ - The left-hand side of an assignment expression may not be
// an optional property access.
// Practically the same as:
(foo === undefined || foo === null
? undefined
: foo.bar === undefined || foo.bar === null
? undefined
: foo.bar.baz) = foobarbaz
To set this property, you must check that the value of foo.bar.baz
is not nullish.要设置此属性,您必须检查
foo.bar.baz
的值是否为空。 This can be done by wrapping the assignment in an if
statement:这可以通过将赋值包装在
if
语句中来完成:
foo.bar.baz = something; // error!
if(foo?.bar?.baz) {
foo.bar.baz = something; // OK
}
If you know that your value isn't null or undefined but the compiler isn't able to determine this on its own, use the non-null assertion operator ( ..
) :如果您知道您的值不是 null 或 undefined 但编译器无法自行确定,请使用非空断言运算符 (
..
) :
foo!.bar!.baz = something; // OK
Here is a playground demonstrating each of these senarios. 这是一个展示这些场景的游乐场。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.