Skip to content

prefer-optional-chain: false positive on tricky condition #829

@christophehurpeau

Description

@christophehurpeau

Here is the condition:

const isSameProjectAsPreviousIntervention = Boolean(
      intervention.projectId &&
      previousIntervention?.projectId?.toString() ===
        intervention.projectId.toString(),
    );

currently, typescript-eslint does not report any issue, which is correct. tsgolint report an issue:

  × typescript-eslint(prefer-optional-chain): Prefer using an optional chain expression instead, as it's more concise and easier to read.

 80 │         const isSameProjectAsPreviousIntervention = Boolean(
 81 │ ╭─▶       intervention.projectId &&
 82 │ │         previousIntervention?.projectId?.toString() ===
 83 │ ╰─▶         intervention.projectId.toString(),
 84 │         );
    ╰────

If written like this:

const isSameProjectAsPreviousIntervention =       previousIntervention?.projectId?.toString() ===
        intervention?.projectId?.toString();

and if both intervention.projectId and previousIntervention?.projectId are null, isSameProjectAsPreviousIntervention is true, but it should be false.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions