'Optional-chaining in Angular

I'm using optional chaining on an object which in turn gives this error after compiling the code

For example

const someObj = {pet_animals: {'dog', 'cat'}};
const duplicate = someObj?.wild_animals;

tsconfig file

enter image description here

Error message

enter image description here



Solution 1:[1]

optional chaining was introduced in ES2020 freeCodecamp.org. If you are your using js version before ES2020, you will get that error.

That might be the reason why you couldn't use optional chaining,

solution:

duplicate = someObj.wild_animals ? someObj.wild_animals : undefined 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vinay Somawat