简体   繁体   English

尝试初始化嵌套数组时出错“赋值表达式的左侧可能不是可选的属性访问。”

[英]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 or undefined .在其核心,可选链接让我们编写代码,如果我们遇到nullundefined , 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.

相关问题 数组赋值中的引用错误(左侧对齐) - Reference error (left-hand asignment) in array assignment 为什么赋值的左侧不能是增量表达式? - Why can't the left hand side of an assignment be an increment expression? 获得C错误“分配给具有数组类型的表达式” - Getting C error “assignment to expression with array type” 尝试访问嵌套在数组中的 object 的属性时未定义的类型 - Typeof undefined when trying to access property of object nested in array 左侧必须是变量(ArrayList和Array) - The left hand side must be a variable (ArrayList and Array) PostgreSQL 检查数组是否包含左侧数组中的任何元素 - PostgreSQL check if array contains any element from left-hand array 参数中的左侧无效 - 参考错误 - Invalid left hand side in argument - Reference error 在循环中分配变量之前评估右侧表达式 - Evaluation of the right hand side expression before assignment to variable in a loop 赋值运算符左侧的递增数组索引 - increment array index left side of assignment operator 获取:“尝试获取非对象的属性”时尝试访问对象数组中的值 - Getting: ' Trying to get property of non-object' when trying to access the value in an array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM