[prefer-optional-chain] include mixed "nullish comparison style" chains in checks #7170
Labels
accepting prs
Go ahead, send a pull request that resolves this issue
enhancement
New feature or request
package: eslint-plugin
Issues related to @typescript-eslint/eslint-plugin
#6397 (comment)
In v6 we have rewritten the
prefer-optional-chain
rule from the ground up.The new logic assesses the "nullish comparison style" of logical operand in relation to the logical operator to determine if it should be consider part of a chain. EG
||
will only assess "equal nullish" checks, and&&
will only assess "not equal to nullish" checks.This does lead to one hole in the rule:
In the first case
foo.bar
is seen as a "truthy boolean" check (eg "not equal nullish") - which isn't a valid operand in an "OR" chain.Similarly in the second case
!foo.bar
is seen as a "falsy boolean" check (eg "equal nullish") - which isn't a valid operand in an "AND" chain.This means that the rule will ignore it and thus will not include it in the chain to report and fix.
This logic is correct and sound - however it doesn't properly encapsulate developer intent. Specifically when that "invalid" chain operand is the very last member in a chain - in certain cases we will actually want to include it as part of the chain.
A slightly longer example:
I think there's a subset of checks that it should include:
foo
and!foo
)!==
/===
/!=
/==
comparing against anything excepttypeof
typeof
becausefoo && typeof foo.bar
will returnundefined | typeof foo.bar
whereastypeof foo?.bar
will returntypeof foo.bar | 'undefined'
- which is VERY different>
/>=
/<
/<=
because TS will hard error if one of the operands is nullish.The text was updated successfully, but these errors were encountered: