Skip to content

Indirectly changing values within switch cases causes an 'unintentional comparison' error #60326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ephf opened this issue Oct 23, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@ephf
Copy link

ephf commented Oct 23, 2024

🔎 Search Terms

switch, case, indirect, unintentional, comparison, change, equality, update, modify, if

🕗 Version & Regression Information

This is the behavior in every version I tried, and I could not find FAQ entries related to the issue.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.0-beta#code/DYUwLgBAHhC8EFYDcAoAZgVwHYGMwEsB7LCHACwEMsBzEAfSgAoBKCAb2jggEYAmJCAF8UKAM4B3fGHKMorNighLSFUSEQAuRcp3kqtBi1TadS-Gllx4feRAD0AKggA6VxAd2hKQUA

💻 Code

let x = 5;
function change_x() { x = 12; }

switch(x) {
    case 5:
        change_x();

        if(x == 12) { /* ... */ }
}

🙁 Actual behavior

bug.ts:8:12 - error TS2367: This comparison appears to be unintentional because the types '5' and '12' have no overlap.

8         if(x == 12) { /* ... */ }
             ~~~~~~~

This error should not show when compiling as x is set to 12 when calling the change_x function.

🙂 Expected behavior

It is expected that there is no error given when compiling.

Additional information about the issue

This can be navigated around by introducing a second variable before the switch statement.

let x = 5;
function change_x() { x = 12; }

let y = x; // here
switch(y) {
    case 5:
        change_x();

        if(x == 12) { /* ... */ }
}

This code no longer produces an error in the latest version

see playground

@MartinJohns
Copy link
Contributor

Duplicate of #9998.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 31, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants