no-unsafe-optional-chaining is an amazing eslint rule, which help us identify why we are doing wrong with optional chaining..
I have one case where i need to perform a sort operation on id (in numbers) which could be in string type which make sense to throw error because the it could result in NaN if order is not a number
const sortComparer = (a, b) => (+a?.order) - (+b?.order)
However, if i do handle no-unsafe-optional-chaining as follow it still throws error and does not work:
const sortComparer = (a, b) => (+a.order || 0) - (+b.order || 0),
The only way to make it work is like this:
const sortComparer = (a, b) => (a?.order ? +a.order : 0) - (b?.order ? +b.order : 0),
Does anyone know how to fix this in a better way to avoid this issue ?
This should work:
sortComparer: (a, b) => +(a?.order || 0) - +(b?.order || 0),
Move the + out of the (), and use the optional chaining to handle a or b being null.