Optional chaining has made life of
However, here we are NOT to discuss how awesome it is, rather solving a issue how of to safely use it.
no-unsafe-optional-chaining is an amazing
eslint rule, which help us identify what are we doing wrong with optional chaining and disallows use of optional chaining in contexts where the
undefined value is not allowed.
I have one case where I need to perform a
sort operation on
id which could be in
string type which make sense to throw error because it could result in
order is not a number as follows:
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 ?