获取以下代码片段的错误
const index = this.dishIds?.indexOf(dishId);
this.prev= this.dishIds?[(this.dishIds?.length + index - 1) % this.dishIds?.length];
this.next= this.dishIds?[(this.dishIds?.length + index + 1) % this.dishIds?.length];
错误:使用这些加载程序处理了文件:
const index = (\_a = this.dishIds) === null || \_a === void 0 ? void 0 : \_a.indexOf(dishId); this.prev = this.dishIds ? [(((\_b = this.dishIds) === null || \_b === void 0 ? void 0 : \_b.length) + index - 1) % ((\_c = this.dishIds) === null || \_c === void 0 ? void 0 : \_c.length)] : ; this.next = this.dishIds ? [(((\_d = this.dishIds) === null || \_d === void 0 ? void 0 : \_d.length) + index + 1) % ((\_e = this.dishIds) === null || \_e === void 0 ? void 0 : \_e.length)] : ;
VS代码编辑器中的错误:
':‘expected.ts(1005)
我相信我只需要在表达式的末尾加上分号。不确定还应该在代码中添加什么。阅读其他线程,我认为这可能是一个tsconfig问题,但不确定我需要修改什么。会很感激你的帮助。
谢谢,
发布于 2022-03-07 07:27:25
您打算使用的运算符称为可选链,在语法上表示为?.
而不是?
。
?
被称为条件(三元)算子。
在错误表示为':' expected.ts(1005)
的情况下,运算符将被视为Conditional (Ternary) Operator
而不是Optional Chaining
运算符。若要将其视为optional chaining
,则必须在?
旁边使用.
例如..。
dishIds?.[(dishIds?.length + index - 1) % dishIds?.length]; //<-- Mark the ?. after dishIds?.[......]
dishIds?.[(dishIds?.length + index + 1) % dishIds?.length];
WYSIWYG
=> WHAT YOU SHOW IS WHAT YOU GET
https://stackoverflow.com/questions/71377015
复制相似问题