Skip to content

Detect no overlap for "in" keywordΒ #61874

Open
@StevenBrons

Description

@StevenBrons

πŸ” Search Terms

"in keyword", "no overlap"

βœ… Viability Checklist

⭐ Suggestion

Extend the "This comparison appears to be unintentional because the types have no overlap" error to the in keyword.

πŸ“ƒ Motivating Example

When you compare values with types that have no overlap, e.g. in the following example:

const a = "foo";
const b = "bar";

if (a === b) {
}

We get the nice error message This comparison appears to be unintentional because the types '"foo"' and '"bar"' have no overlap.(2367).

The same does unfortunately not hold for the in keyword, which is sometimes confused when used in combination with arrays:

const a = "foo";
const b = ["foo"] as const;

if (a in b) {
}

(for those that fall in the trap, the in keyword compares the keys of ["foo"], so 0, length etc.)
It would be a logical extension of the other error to also include this case.

πŸ’» Use Cases

As highlighted above, the current behavior leaves errors on the table that should be fixed. Workarounds can be achieved using linting (gpt believes https://typescript-eslint.io/rules/no-unnecessary-condition/)

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions