Home > Software engineering >  Getting Lint No Nested Ternary Error when using ternary operator
Getting Lint No Nested Ternary Error when using ternary operator

Time:07-12

What is the best way to avoid receiving the error no nested ternary on typescript.

                  disablePortal
                  options={
                    // eslint-disable-next-line no-nested-ternary
                    units=== "mm"
                      ? valuesMm
                      : units === "km"
                      ? valueskm
                      : valuesls
                  }

I tried using this but it is giving an error

options={
                    units=== "mm"
                      ? valuesMm
                      : [
                          units.slumpUnit === "cm"
                            ? valuesCm
                            : valuesIn,
                        ]
                  }

CodePudding user response:

I could easily see this as a place one could decide to ignore the rule and nest it - but if you don't want the nesting, another option is to put the logic into an IIFE and return the values conditionally.

options={(() => {
  if (units=== 'mm') return valuesMm;
  if (units === 'km') return valueskm;
  return valuesls;
})()}
  • Related