Getting error for the following snippet of code
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]; Error: File was processed with these loaders:
- ./node_modules/@angular-devkit/build-angular/src/babel/webpack-loader.js
- ./node_modules/@ngtools/webpack/src/ivy/index.js You may need an additional loader to handle the result of these loaders. | var _a, _b, _c, _d, _e;
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)] : ;
Error in the VS Code editor:
':' expected.ts(1005)
I believe I need only the semi-colon at the end of the expression which has been added. Not sure what else should be added to the code. Reading other threads, I believe it might be a tsconfig issue, but not sure what I need to modify. Would appreciate the help.
Thanks,
1 Answer
The operator that you intend to use is called Optional Chaining and which is syntactically expressed as ?. and not ?
? is called Conditional (Ternary) Operator.
In the case where the error says ':' expected.ts(1005), the operator is getting treated as a Conditional (Ternary) Operator and not as Optional Chaining operator. For it to be treated as optional chaining, you will have to use a . beside ?
For example...
dishIds?.[(dishIds?.length + index - 1) % dishIds?.length]; //<-- Mark the ?. after dishIds?.[......] dishIds?.[(dishIds?.length + index + 1) % dishIds?.length];