Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

optional chaining in javascript causing error in eslint

i tried to use optional chaining in javascript but my eslint rules causing error .

Error : Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError no-unsafe-optional-chaining

const { homeAddress, officeAddress} = Employee?.addresses;

Error : Unsafe arithmetic operation on optional chaining. It can result in NaN no-unsafe-optional-chaining

const AddressesCount = homeAddress?.length + officeAddress?.length 

how can I fix this issue ? I do not want to violate the rule

like image 979
V2rson Avatar asked Apr 20 '26 12:04

V2rson


1 Answers

There are multiple ways to fix this, or you can use /* eslint-disable-next-line no-unsafe-optional-chaining */. Which I would not advise but it will fix the errors. The best way to fix this in my opinion is by using const { homeAddress, officeAddress } = Employee?.addressess || {};. What you can also try is const { addresses: { homeAddress, officeAddress } } = Employee;

like image 99
Lars Vonk Avatar answered Apr 23 '26 04:04

Lars Vonk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!